1

I have the below cron.txt file

58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null

And I need to comment (add #) at the beginning of all the lines that doesn't have pattern CXC or HP in it.

I tried

grep -iwvE "CXC|HP" cron.txt | sed 's/^/#/g' > cron.txt_bkp

It didn't work as expected.

3 Answers 3

4

Assuming that your implementation of grep supports both the -w and the -E flag, your grep and sed pipeline would only extract and comment out the lines that didn't contain HP or CXC. The lines that were to be left as-is will not be transferred to the new file.


Since HP-UX sed seems to not support alteration with | in regular expressions, which means that /CXC|HP/!s/^/# / won't work, you may apply something like this to the file:

sed -e '/CXC/b' -e '/HP/b' -e 's/^/# /' crontab.txt >crontab-new.txt

The b command makes sed branch to the end of the editing script if the pattern is found on the current line. It acts as a "print line and continue with next line" command here.

If neither of the first two expressions are acted upon, then the line is commented out by the last expression.

The file crontab-new.txt, created above, will hold the following contents given the example in the question:

58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
# 00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
# 01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
# 01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
# 01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null
2
  • 1
    I think you misread the question. You got it backwards.
    – Wildcard
    Jan 30, 2018 at 7:39
  • @Wildcard Now properly corrected.
    – Kusalananda
    Jan 30, 2018 at 8:47
1

A solution with awk:

awk '!/CXC|HP/{$0="#" $0}1' cron.txt > cron.txt_bkp
1
  • 1
    gsub() could be changed to sub(). There is only one single match of ^ in a line... Alternatively, into just $0 = "#" $0.
    – Kusalananda
    Jan 30, 2018 at 10:57
0

I have used below sed command to achieve the same

cron.txt

58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null

command

sed '/HP/!s/^/#/g' cron.txt | sed '/CXC/s/^#//g' >cron.txt_bkp

output

58 18 * * 1-5 /usr/home/script.sh REP CXC BS TODAY all 1>/dev/null 2>/dev/null
#00 19 * * 1-5 /usr/home/script.sh DSC DXC BUS TODAY all 1>/dev/null 2>/dev/null
01 19 * * 1-5 /usr/home/script.sh REP HP SNT TODAY all 1>/dev/null 2>/dev/null
03 19 * * 1-5 /usr/home/script.sh DSC CXC SNT TODAY all 1>/dev/null 2>/dev/null
32 10 * * 1-5 /usr/home/script.sh Check CXC OD TODAY MGLA 1>/dev/null 2>/dev/null
32 12 * * 1-5 /usr/home/script.sh Sca CXC OD TODAY all "01 03 05 07 08 10 12 17 18 19 31 32 33 37 42 50 53 55 57 84 89 93" 1>/dev/null 2>/dev/null
#01 19 * * 1-5 /usr/home/script.sh REP HK SNT TODAY all 1>/dev/null 2>/dev/null
#01 19 * * 1-5 /usr/home/script.sh REP RAM SNT TODAY all 1>/dev/null 2>/dev/null
#01 19 * * 1-5 /usr/home/script.sh REP SAB SNT TODAY all 1>/dev/null 2>/dev/null
2
  • This will definitely work, but it's a tiny bit roundabout way of doing it (adding, then removing comments).
    – Kusalananda
    Jan 31, 2018 at 9:13
  • Kindly vote for my answer Jan 31, 2018 at 9:52

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.