Comment all lines which are not already commented for each full path pattern matched


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comment all lines which are not already commented for each full path pattern matched
# 1  
Old 01-27-2019
Comment all lines which are not already commented for each full path pattern matched

Hello.


Question 1 :

I want to comment out all lines of a cron file which are not already commented out for each full path pattern matched.


Example 1 nothing to do because line is already commented out; pattern = '/usr/bin/munin-cron'

Code:
# */5 * * * *     munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron


Example 2 line is being commented out; pattern = '/usr/bin/munin-cron'
before :
Code:
*/5 * * * *     munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron

after :
Code:
# */5 * * * *     munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron


I have tried this but it is not working :
Code:
sed -i '\|/usr/bin/munin-cron\| s|^|#|' /etc/cron.d/munin

Got error :
Code:
sed: -e expression #1, char 27: unknown command: `^'


Question 2

And the reverse command
I want to uncomment all lines of a cron file which are not already uncommented for each full path pattern matched.

Example 1 nothing to do because line is already uncommented ; pattern = '/usr/bin/munin-cron'

Code:
*/5 * * * *     munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron


Example 2 line is being uncommented ; pattern = '/usr/bin/munin-cron'
before :
Code:
#*/5 * * * *     munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron


or before :
Code:
# */5 * * * *     munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron


after :
Code:
*/5 * * * *     munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron

Any help is welcome

Last edited by RavinderSingh13; 01-27-2019 at 02:00 PM..
# 2  
Old 01-27-2019
You escaped one address delimiter too many. man sed:


Quote:
Addresses
\cregexpc
Match lines matching the regular expression regexp. The c may be any character.
Try your sed command again, with the second | unescaped.


To avoid commenting out an already commented line, you can either add the # to all matching lines, and then remove duplicates, or extend the address, like
Code:
sed '\|^[^#].*/usr/bin/munin-cron| s|^|#|' file

Using a similar address for the reverse operation, including 0 - n spaces in the s command, is left as an execise for the reader.

Last edited by RudiC; 01-28-2019 at 08:59 AM..
These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 01-28-2019
Quote:
Originally Posted by RudiC
You escaped one address delimiter too many. man sed:


Try your sed command again, with the second | unescaped.


To avoid commenting out an already commented line, you can either add the # to all matching lines, and then remove duplicates, or extend the address, like
Code:
sed '\|^[^#].*/usr/bin/munin-cron| s|^|#|' file

Using a similar address for the reverse operation, including 0 - n spaces in the s command, is left as an execise for the reader.

Thank you very much.


What is the meaning of : ^[^#] in '\|^[^#].*.................
# 4  
Old 01-28-2019
It is a "bracket expression" at begin-of-line ("^", as you used it in your substitution). man regex:

Quote:
A bracket expression is a list of characters enclosed in "[]". It normally matches any single character from the list (but see below). If the list begins with '^', it matches any single character (but see below) not from the rest of the list.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace the path value in files, pattern is not full known.

Hi, I need to do find and replace, but the pattern is not full known. for example, my file has /proj/app-d1/sun or /data/site-d1/conf here app-d1 and site-d1 is not constant. It may be different in different files. common part is /proj/xx/sun and /data/xxx/conf i want to find where ever... (6 Replies)
Discussion started by: rbalaj16
6 Replies

2. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

3. Shell Programming and Scripting

Delete lines and the first pattern between 2 matched patterns

Hi, i need help to delete all the lines between 2 matched patterns and the first pattern must be deleted too. sample as follows: inputfile.txt >kump_1 ........................... ........................... >start_0124 dgfhghgfh fgfdgfh fdgfdh >kump_2 ............................. (7 Replies)
Discussion started by: redse171
7 Replies

4. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

5. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

6. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

7. Shell Programming and Scripting

removing lines around a matched pattern

I have an ugly conf file that has the string I'm interested in searching for in the middle of a block of code that's relevant, and I'm trying to find a way to remove that entire block based on the matched line. I've googled for this problem, and most people helping are only interested in... (9 Replies)
Discussion started by: tamale
9 Replies

8. Shell Programming and Scripting

How to extract strings from full path when full path is not fixed

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

9. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question