How To comment a line where a word exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How To comment a line where a word exists
# 1  
Old 10-29-2009
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 directory 3
monitor 192.168.1.14 Copying files in current directory 4
monitor 192.168.1.15 Copying files in current directory 5
monitor 192.168.1.12 Copying files in current directory 6
monitor 192.168.1.166 Copying files in current directory 7
monitor 192.168.1.12 Copying files in current directory 8
monitor 192.168.1.112 Copying files in current directory 9

so what is command to comment (#) the lines where the word "192.168.1.12" exists in file /tmp/file.txt

Thx.
Darren
# 2  
Old 10-29-2009
Code:
# awk '/192.168.1.12/ { $0="#"$0 } 1' /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 directory 3
monitor 192.168.1.14 Copying files in current directory 4
monitor 192.168.1.15 Copying files in current directory 5
#monitor 192.168.1.12 Copying files in current directory 6
monitor 192.168.1.166 Copying files in current directory 7
#monitor 192.168.1.12 Copying files in current directory 8
monitor 192.168.1.112 Copying files in current directory 9

or
sed -i "s/.*192.168.1.12.*/#&/" /tmp/file.txt

# 3  
Old 10-29-2009
Bug

Thank You. The sed example did exactly what I wanted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

2. Shell Programming and Scripting

sed to add word if pattern exists

Hi, I have a file with data listed below: clm_id double, cnl_id double, cnl_amt double I want output to be clm_id string, cnl_id string, cnl_amt double So replace double by string if the field contains the word _id I tried sed -e 's/_id/& string/' filename The above will not... (2 Replies)
Discussion started by: wahi80
2 Replies

3. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

5. Shell Programming and Scripting

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. exclude exclude= hi I am excluded excludes excludes= ... (9 Replies)
Discussion started by: anil510
9 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

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 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. UNIX for Dummies Questions & Answers

Need to serach if a new line character exists on the last line in a file

I have a file in which I need to search if a new line character exists on the last line in the file. Please let me know how can I achieve it using Unix commands? (10 Replies)
Discussion started by: sunilbm78
10 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question