简体   繁体   中英

Problems with shell script (sed) in bash

I am attempting to run the following script in 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

Each line has been tested separately and functions as it should, except when put together into a script.

The first file seems to be just fine. The next 4 files are blank. Then the next 2 files are good. This keeps up at seemingly random intervals throughout the 550 files that I need to run this on.

Any Ideas?

Thanks.

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

You probably mean

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

Even further

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

Which would include those lines with only spaces.

On a test, this command

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

Shows

a
b

While this command

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

Shows nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM