comment a line of the patterns is a the beginning of the line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comment a line of the patterns is a the beginning of the line
# 1  
Old 08-27-2012
comment a line of the patterns is a the beginning of the line

I need to comment the lines starting with pattern "exclude" or "exclude=". If the work exclude comes at any other part, ignore it. Also, ignore, excludes, excluded etc. Ie only comment the line starting with exclude.

File contents.
Code:
exclude
exclude=
hi I am excluded
excludes
excludes=

Desired output

Code:
#exclude
#exclude=
hi I am excluded
excludes
excludes=

# 2  
Old 08-27-2012
Hi


Code:
$ awk -F= '$1 == "exclude"{printf "#";}1' file
#exclude
#exclude=
hi I am excluded
excludes
excludes=

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 08-27-2012
Works great Smilie
# 4  
Old 08-27-2012
You can also try
Code:
sed 's/^exclude/#/g' filename


Last edited by Franklin52; 08-27-2012 at 09:10 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 08-27-2012
Quote:
Originally Posted by sathyaonnuix
You can also try


sed 's/^exclude/#/g' filename
That will not produce the output desired. Have you tried it?
# 6  
Old 08-27-2012
Ah how i read it incorrectly.. Hope this works..
Code:
sed 's/\(exclude\)/#\1/g;s/\(exclude=\)/#\1/g'

@Elixir: Am on my mobile.. couldn't try..

Last edited by Franklin52; 08-27-2012 at 09:10 AM.. Reason: code tags
# 7  
Old 08-27-2012
Quote:
Originally Posted by sathyaonnuix
Ah how i read it incorrectly.. Hope this works..

sed 's/\(exclude\)/#\1/g;s/\(exclude=\)/#\1/g'

@Elixir: Am on my mobile.. couldn't try..
That will not work either.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

2. Shell Programming and Scripting

How to comment a specific line of a file?

Hi, I need to comment out (insert # in the front of a line) a line that has entry Defaults requiretty using command-line as I need to do this on hundreds of servers. From Defaults requiretty To #Defaults requiretty I tried something like below but no luck: Please advise,... (3 Replies)
Discussion started by: prvnrk
3 Replies

3. Shell Programming and Scripting

beginning less from line #

Hi from a script i want to to read a file beginning at line e.g. number 21 to the EOF. less +n21 temp.txt Bevor the result, it brings an empty page, so that i cant use for scripting. Any idea how the problem can be solved? Thanks in advance! IMPe (2 Replies)
Discussion started by: IMPe
2 Replies

4. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

5. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

6. Shell Programming and Scripting

Comment a line with SED

I have around 25 hosts and each hosts has 4 instance of jboss and 4 different ip attached to it . I need to make some changes to the startup scripts. Any tips appreciated. I have total of 100 instances which bind to 100 different ip address based on instance name. For example File1 ... (1 Reply)
Discussion started by: gubbu
1 Replies

7. Shell Programming and Scripting

How To comment a line where a word exists

Hi All Can u help me.. My problem is comment (#) a line where a word exists in that line sample: cat /tmp/file.txt monitor 192.168.1.11 Copying files in current directory 1 monitor 192.168.1.1 Copying files in current directory 2 monitor 192.168.1.12 Copying files in current... (2 Replies)
Discussion started by: darren_j
2 Replies

8. UNIX for Dummies Questions & Answers

how to replace a text of line with a comment line

I want to replace this line : "test compare visible] true" and make it "#test compare visible] true". How can I do it ? And it should be checked in many sub folder files also. (6 Replies)
Discussion started by: manoj.b
6 Replies

9. Shell Programming and Scripting

Placing a comment at the beginning of a line

Hello - I am running Linux. I want to place a comment char at the beginning of a line in a file. For example: testvar=`grep username /etc/people sed -e 's/$testvar/#$testvar/g' /etc/people I cannot get the above commands to put a comment at the beginning of the line. Any... (3 Replies)
Discussion started by: mlike
3 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question