1

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.

1
  • 1
    What are you trying to do? Please show us your input file and desired output Commented Sep 10, 2013 at 18:18

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

6 Comments

That was my original command, but it didn't work properly. I deleted the subsequent pages in a multi-page document, but not the lines at the bottom of the first page. I was given ^.$ from another member here and to correct that issue.
@DominicRomano On my test it works as intended. Even on a file.
I appreciate the help, but the line '/^$/,$d' doesn't work on my system. I need to use '/^.$/,$d' in order to get the desired results.
@DominicRomano I think that ('/^.$/,$d') would actually work if your files are in CRLF format, but the proper way for that is '/^\r$/,$d'. Probably when you were testing /^$/,$d you were actually doing it on a CRLF file? Also a compatible approach is /^\r\?$/,$d which would apply on either formats. For including lines with only spaces, use '/^[[:space:]]*$/,$d'.
@DominicRomano I hope you could try at least '/^\r\?$/,$d' or '/^[[:space:]]*$/,$d' and tell us how it goes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.