954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sed leaves file empty

Hello all, new to this site and to scripting.

I am trying to search for the line with "CALIBRATE" in a file, and remove the leading # from the line with the following lines:

cat $F1 | sed -e '/CALIBRATE/s/^#//' > $F1
cat $F2 | sed -e '/CALIBRATE/s/^#//' > $F2
cat $F3 | sed -e '/CALIBRATE/s/^#//' > $F3

Another part of my script does basically the same thing in reverse, that is, add the leading # to the line. However, when the script is run several times in a row with the different options, one of the output files are left either empty or scrambled (seen as binary).
My guess is that something happens when run again and there is no # to remove at the beginning of the line.

Any help would be much appreciated.

PG

dude67
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

First, your problem: you can't use the same file for input and output (using > at least; using >> will work, but it will append the data which you also don't want).*

Second, sed takes a file as a final parameter, so you dont need cat | sed ...

* sed also has an in-place option, allowing you to use the same file for input and output. If you use the -i option, it will do this. The actual form (copying from the man page on my other comp) is -i[SUFFIX] where SUFFIX is optional, but when given it will copy the original file to fileSUFFIX, e.g. sed -i.bkup s/a/b/ somefile will create a file somefile.bkup without the changes.

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

Hello all, new to this site and to scripting.

I am trying to search for the line with "CALIBRATE" in a file, and remove the leading # from the line with the following lines:

cat $F1 | sed -e '/CALIBRATE/s/^#//' > $F1 cat $F2 | sed -e '/CALIBRATE/s/^#//' > $F2 cat $F3 | sed -e '/CALIBRATE/s/^#//' > $F3

Another part of my script does basically the same thing in reverse, that is, add the leading # to the line. However, when the script is run several times in a row with the different options, one of the output files are left either empty or scrambled (seen as binary). My guess is that something happens when run again and there is no # to remove at the beginning of the line.

Any help would be much appreciated.

PG

This might help.

if [ -L "$fullpath" ]; then #fullpath="`ls -l "$fullpath" | awk '{print $11}'`" fullpath=`ls -l "$fullpath" |sed -e 's/.* -> //' |sed -e 's/\*//'` fi dirname $fullpath } # Set the home if not already set
indie
Newbie Poster
3 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

Thanks, I will give it a try...

dude67
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

This might help.

if [ -L "$fullpath" ]; then #fullpath="`ls -l "$fullpath" | awk '{print $11}'`" fullpath=`ls -l "$fullpath" |sed -e 's/.* -> //' |sed -e 's/\*//'` fi dirname $fullpath } # Set the home if not already set


Next time please post your code more readably, or with a brief explanation of what it does. I can't imagine how it pertains to the OP's problem.

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

man sed
especially look at -i option
sed -i'' b -e 's/a/b/'

pheeror
Light Poster
42 posts since Mar 2007
Reputation Points: 22
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You