1

我正在尝试在 bash 中运行以下脚本:

#! /bin/Bash

cp '../Text_Files_Backups/'*.txt .

sed -i '1,/Ref/d' *.txt

##Deletes all lines from the begining of file up to and including the line that includes the text 'Ref'
##      

sed -i -b '/^.$/,$d' *.txt
##Deletes all blank lines and text following and including the first blank line

sed -i 's/\([(a-zA-Z) ]\)\([(1-9)][(0-9)][ ][ ]\)/\1\n\2/g' *.txt
##Searches document for any instance of a letter character immediately followed by a 2 digit number ##immediately followed by 2 blank spaces
##  <or>
##a blank space immediately followed by a 2 digit number immediately followed by 2 blank spaces
##      and inserts a new line immediately prior to the 2 digit number

exit

每一行都经过单独测试,并按其应有的功能运行,除非将它们组合成一个脚本。

第一个文件似乎很好。接下来的 4 个文件是空白的。然后接下来的2个文件很好。这在我需要运行它的 550 个文件中以看似随机的间隔保持不变。

有任何想法吗?

谢谢。

4

1 回答 1

2
sed -i -b '/^.$/,$d' *.txt
##Deletes all blank lines and text following and including the first blank line

你大概是说

sed -i -b '/^$/,$d' *.txt

更深入

sed -i -b '/^[[:blank:]]*$/,$d' *.txt

这将包括那些只有空格的行。

在测试中,此命令

(echo a; echo b; echo; echo b; echo; echo; echo c; echo d) | sed '/^$/,$d'

节目

a
b

虽然这个命令

(echo a; echo b; echo; echo b; echo; echo; echo c; echo d) | sed '/^.$/,$d'

什么都不显示。

于 2013-09-10T18:19:01.593 回答