To do some action on some specific lines, some pattern has to be identified for them. Say we want to comment all lines where "pc" is a substring of script name, then:
sed '/pc/ s!.*!#&!' cron_file
Still your overall requirements are not clear. Input file is known but you need multiple processing on that file. You need to mention everything you need to do on that file along with complete expected output.
OK, so here you have only one requirement. In my previous post, I was talking about many more processing on crontab files mentioned in post #1.
if -i switch is supported:
sed -i 's!.*\/dev1\/scripts\/.*!#&!' cronfile.txt
If not:
sed 's!.*\/dev1\/scripts\/.*!#&!' cronfile.txt > temp; mv temp cronfile.txt
@Scrutinizer - Your solution commenting out all the entries in cron file which i do not want to do. I want to comment only the entries matching the value in $script_path which is /dev1/scripts/
Don't forget to set the variable $script_path first..
---------- Post updated at 22:02 ---------- Previous update was at 21:51 ----------
Sure, with the substitution operator s you can use any character (except backslash and newline ) as a delimiter so instead of / I used | .
The pattern to match is .*${script_path}.* which matches the whole line if the contents of $script_path are part of the line. If the latter is the case then it gets replaced by a # , followed by the line that was matched ( & ) . Everything is in double quotes so that variables get expanded by the shell.
See the variable name in your command..
${script_path}
it's different than the one you exported (scripts_path).Correct the variable name in sed command.
I also need to remove the comment from the entries after some operation. Probably i am going to use another script for this. Can you guide me on this?
i think i can use 'd' option to remove the comments. but not sure how to do it.
sed -e 's/#.*//'
but i guess this will remove the '#' from all other entries which someone might have commented out. Can you let me know how to remove the comments for the entries which i commented out?