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

Recommended Answers

All 5 Replies

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

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

Thanks, I will give it a try...

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.