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.

Kusalananda
  • 333,661
  • In order to edit your question and accept an answer, you should register this account (https://unix.stackexchange.com/users/327289/user327289) - it appears you've accidentally created two separate accounts to reply (1 and 2) – Jeff Schaller Dec 17 '18 at 18:18

2 Answers2

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

Kusalananda
  • 333,661
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