0

I have a text file named crn.txt containing text below:

9 1 * * 3,6 /opt/testtingtools/kos/bin/cos.sh
55 23 * * * /opt/testtingtools/tqdaily.sh 2>>/opt/toolcheck/extract.err
50 11 * * 6 /opt/devtools/toolbox/toolcheck.sh >>toolcheck.log 2>&1
55 23 * * 5 /opt/devtools/toolbox/reset.sh >>/opt/toolcheck/log/reset.log
56 23 * * 6 /opt/prdtools/tqweekly.sh 2>>/opt/checktool/extract.err
30 11 * * 6 /opt/proadtools/tool.sh >/opt/checkingtools/tool.log 2>&1

I need to delete the lines containing a word testtingtools and update crn.txt so that the output looks like this:

50 11 * * 6 /opt/devtools/toolbox/toolcheck.sh >>toolcheck.log 2>&1
55 23 * * 5 /opt/devtools/toolbox/reset.sh >>/opt/toolcheck/log/reset.log
56 23 * * 6 /opt/prdtools/tqweekly.sh 2>>/opt/checktool/extract.err
30 11 * * 6 /opt/proadtools/tool.sh >/opt/checkingtools/tool.log 2>&1

I am using the command

sed '/testtingtools/d' crn.txt 2>&1 | tee crn.txt

It works in bash or the command line, but not inside the script. I am using unix server (sun solaris).

One more command working in linux but not in unix:

echo "$(sed '/testtingtools/d' crn.txt)" > crn.txt

"Not working" means it is not deleting a particular line, it empties the entire file when I use the code inside the script. But when I use the code in command line, it deletes the line containing testting word from crn.txt.

1
2

Solaris sed can not do in-place editing.

On Linux, you would have used

sed -i '/testtingtools/d' crn.txt

The portable way of doing it, which would work on both Solaris and Linux, is

cp crn.txt crn.tmp
sed '/testtingtools/d' <crn.tmp >crn.txt &&
rm crn.tmp

What's probably happening for you is that tee truncates the file before sed has a chance of reading from it, resulting in an empty file. Commands in a pipeline are run concurrently.

In general you'd want to avoid reading from a file that you are truncating in the same command, and instead use a temporary file. This is what sed -i does behind the scenes.

Likewise with your other command:

echo "$(sed '/testtingtools/d' crn.txt)" > crn.txt

which is better written as

sed '/testtingtools/d' crn.txt >crn.txt

The first thing that would happen in any standard shell (on Linux and on Solaris), is that the shell sees the redirection and truncates the output file to zero size. Then it executes sed. This command would not work as expected on Linux nor on Solaris (if you expect it to edit the original file, that is).

3
  • 1
    no -i in Solaris sed
    – Jeff Schaller
    Dec 17 '18 at 15:26
  • 1
    I always like to plug Scott's version of this: cp input_file temp_file && command … temp_file > input_file && rm temp_file -- and that's what you already did!
    – Jeff Schaller
    Dec 17 '18 at 15:27
  • 1
    @JeffSchaller Almost, apart from the conditionals. It keeps the permissions/ownerships on the original file. Actually, the conditionals are not needed as a failure to cp the file would result in sed not running and the original file being left untouched. Hmm... a && before rm may be prudent however.
    – Kusalananda
    Dec 17 '18 at 15:28
0
We can do it both sed and awk

awk method

awk '!/testtingtools/{print $0}' crn.txt >l.txt &&yes| mv l.txt crn.txt

output

 cat crn.txt
50 11 * * 6 /opt/devtools/toolbox/toolcheck.sh >>toolcheck.log 2>&1
55 23 * * 5 /opt/devtools/toolbox/reset.sh >>/opt/toolcheck/log/reset.log
56 23 * * 6 /opt/prdtools/tqweekly.sh 2>>/opt/checktool/extract.err
30 11 * * 6 /opt/proadtools/tool.sh >/opt/checkingtools/tool.log 2>&1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.