Boardy
Boardy

Reputation: 36205

Removing a line from a file using sed in bash script

I am currently working on a shell script where an error may sometimes get outputted to a file but I need the error moved as it doesn't mean anything and everything works as normal as long the error isn't there.

I have a script which has the following:

ftp ftp://$2:$3@$1 << BLOCK > $5
cd "$4"
ls
quit
BLOCK

$5 is the file that the output of the FTP command is written to. So in the file I have the following

total 8
-rw-r--r--  1 root  root  42 Dec 17 14:26 test1.txt
-rw-r--r--  1 root  root   6 Dec 17 14:08 test2.txt

Sometimes I get the error ?Invalid command. so I am using sed to remove the error from the file. I have changed the shell script to the following. Note for testing I have put error within the FTP to ensure I get the error message mentioned above.

ftp ftp://$2:$3@$1 << BLOCK > $5
cd "$4"
ls
error
quit
BLOCK
fileout=$(sed '/?Invalid command./d' $5)
echo $fileout > $5

This is working in the sense that I am removing the error message but it also removing the line breaks so when I look at the file I get the following

total 8 -rw-r--r-- 1 root root 42 Dec 17 14:26 test1.txt -rw-r--r-- 1 root root 6 Dec 17 14:08 test2.txt

How do I retain line breaks?

Thanks for any help you can provide.

Upvotes: 3

Views: 2355

Answers (2)

William Pursell
William Pursell

Reputation: 212248

Just do:

ftp ftp://$2:$3@$1 << BLOCK | sed ... > $5
...
BLOCK

Or, if you prefer:

ftp ftp://$2:$3@$1 << BLOCK | 
...
BLOCK
sed ... > $5

Upvotes: 0

dogbane
dogbane

Reputation: 274612

You need to quote fileout when you echo i.e. echo "$fileout" > $5, in order to preserve the line breaks.

BUT, instead of doing that, you should simply use sed to edit the file "in-place". Then you wouldn't have to save the output of sed into a variable and then echo it back out again, which is causing the issue.

Use:

sed -i '/?Invalid command./d' $5

instead of:

fileout=$(sed '/?Invalid command./d' $5)
echo $fileout > $5

Upvotes: 3

Related Questions