[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 【已解决】BAT批处理怎样判断为纯英文的行其后面追加数字?

本帖最后由 elec 于 2014-7-8 08:14 编辑

一个文本,里面主要为英文。

判断每一行,如果为纯英文(没有数字【0-9】),就在每行后面追加2014?

如何办到??

  1. for /f "delims=" %%a in ('Findstr "^[a-z]*$" 1.txt') do echo %%a2014 >>new.txt
复制代码

TOP

回复 2# 522235677


    代码测试不成功。未生成new.txt

为啥多了一个1,运行错误和这儿有关?

TOP

本帖最后由 CrLf 于 2014-7-8 00:27 编辑

多的那个 1 代表 StdOut,不影响输出
  1. @echo off
  2. (for /f "delims=" %%a in (a.txt) do (
  3. for /f "tokens=1* delims=0123456789" %%b in ("%%a#") do (
  4. if %%c#==# (echo %%a2014) else (echo %%a)
  5. )
  6. ))>new.txt
复制代码
这用 sed 最合适:
  1. sed "/[0-9]/!s/$/2014/" 1.txt >new1.txt
  2. ::不换行
  3. sed "/[0-9]/!a2014" 1.txt >new2.txt
  4. ::换行
复制代码

TOP

回复 4# CrLf


    为什么您的代码。第一个测试也是不成功?没有任何输出文件。 是保存为bat,双击吧

    sed第一段代码成功,所有输出连在一起了。

    sed第二段代码不成功,输出一个空文件new2.txt

TOP

回复 5# elec


    已修改

TOP

回复 6# CrLf

sed第二个还是出错

您可能没懂我的意思哈。test.txt
  1. 5 died after a missile reportedly struck a smuggling tunnel near Rafah.
  2. The Israeli military said it
  3. had hit "terror sites and concealed rocket launchers"
  4. in 2014 response to rocket and mortar fire from the coastal territory.
  5. Tensions have risen since a Palestinian youth was killed
  6. in apparent reprisal for the murder of three Israelis.
  7. On Sunday, Israeli police said they had arrested 7
  8. Jewish suspects in connection with the death of Mohammed 90's Abu Khdair last week.
  9. Yolande Knell reports from Shufat where a mourning 10 for Mohammed
复制代码
2356行没有数字,输出的格式。。
  1. 5 died after a missile reportedly struck a smuggling tunnel near Rafah.
  2. The Israeli military said it 2014
  3. had hit "terror sites and concealed rocket launchers" 2014
  4. in 2014 response to rocket and mortar fire from the coastal territory.
  5. Tensions have risen since a Palestinian youth was killed 2014
  6. in apparent reprisal for the murder of three Israelis.2014
  7. On Sunday, Israeli police said they had arrested 7
  8. Jewish suspects in connection with the death of Mohammed 90's Abu Khdair last week.
  9. Yolande Knell reports from Shufat where a mourning 10 for Mohammed
复制代码

TOP

回复 7# elec


    好吧,原来“纯英文”是有标点的...已修改

TOP

回复 8# CrLf


    结果只有new1正确
new1.txt
  1. 5 died after a missile reportedly struck a smuggling tunnel near Rafah.
  2. The Israeli military said it 2014
  3. had hit "terror sites and concealed rocket launchers" 2014
  4. in 2014 response to rocket and mortar fire from the coastal territory.
  5. Tensions have risen since a Palestinian youth was killed 2014
  6. in apparent reprisal for the murder of three Israelis.2014
  7. On Sunday, Israeli police said they had arrested 7
  8. Jewish suspects in connection with the death of Mohammed 90's Abu Khdair last week.
  9. Yolande Knell reports from Shufat where a mourning 10 for Mohammed
复制代码
new.txt 第一行有一个5,后面不应该加2014
  1. 5 died after a missile reportedly struck a smuggling tunnel near Rafah.2014
  2. The Israeli military said it 2014
  3. had hit "terror sites and concealed rocket launchers" 2014
  4. in 2014 response to rocket and mortar fire from the coastal territory.
  5. Tensions have risen since a Palestinian youth was killed 2014
  6. in apparent reprisal for the murder of three Israelis.2014
  7. On Sunday, Israeli police said they had arrested 7
  8. Jewish suspects in connection with the death of Mohammed 90's Abu Khdair last week.
  9. Yolande Knell reports from Shufat where a mourning 10 for Mohammed
复制代码
new2.txt 2014不在下一行
  1. 5 died after a missile reportedly struck a smuggling tunnel near Rafah.
  2. The Israeli military said it
  3. 2014
  4. had hit "terror sites and concealed rocket launchers"
  5. 2014
  6. in 2014 response to rocket and mortar fire from the coastal territory.
  7. Tensions have risen since a Palestinian youth was killed
  8. 2014
  9. in apparent reprisal for the murder of three Israelis.
  10. 2014
  11. On Sunday, Israeli police said they had arrested 7
  12. Jewish suspects in connection with the death of Mohammed 90's Abu Khdair last week.
  13. Yolande Knell reports from Shufat where a mourning 10 for Mohammed
复制代码

TOP

返回列表