我有以下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
我需要#
在所有没有模式CXC
或HP
其中没有模式的行的开头进行注释(添加)。
我试过
grep -iwvE "CXC|HP" cron.txt | sed 's/^/#/g' > cron.txt_bkp
它没有按预期工作。
答案1
假设您的实现grep
同时支持-w
和-E
标志,您的grep
和sed
管道只会提取并注释掉不包含HP
或 的行CXC
。原本要保留的行将不会转移到新文件中。
自从惠普-UXsed
似乎不支持|
正则表达式中的更改,这意味着这/CXC|HP/!s/^/# /
不起作用,您可以将类似的内容应用于文件:
sed -e '/CXC/b' -e '/HP/b' -e 's/^/# /' crontab.txt >crontab-new.txt
如果在当前行找到该模式,该b
命令将分支到编辑脚本的末尾。sed
它在这里充当“打印行并继续下一行”命令。
如果前两个表达式均未执行,则该行将被最后一个表达式注释掉。
上面创建的文件crontab-new.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
答案2
awk 的解决方案:
awk '!/CXC|HP/{$0="#" $0}1' cron.txt > cron.txt_bkp
答案3
我使用下面的 sed 命令来实现相同的目的
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
命令
sed '/HP/!s/^/#/g' cron.txt | sed '/CXC/s/^#//g' >cron.txt_bkp
输出
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