nawk oneliner to find and replace a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nawk oneliner to find and replace a pattern
# 1  
Old 04-18-2012
nawk oneliner to find and replace a pattern

Hi,

I need to replace the ip 1.1.1.1 with the name test.sol.box . I have tried and come up with following code.
do we have any other way of doing this with nawk??

Data:
Code:
#This is a test setup.Please enter your values and corresponding port number here
##########################
Server Host = 1.1.1.1
Server Port = 22

Server Host = 1.1.1.1
Server Port = 22


Server Host = 1.1.1.1
Server Port = 22


Code:
nawk '/Server.Host/{FS="=";sub ($2,"test.sol.box")}{print}' test.txt


one more query, i could see the output differs when i place the print statement within the first loop

i.e.
Code:
nawk '/Server.Host/{FS="=";sub ($2,"test.sol.box"){print}' test.txt

does this mean the print executes the line where the pattern match is found ?
# 2  
Old 04-18-2012
awk

Hi,

Try this one,

Code:
nawk 'BEGIN{FS="=";}/Server.Host/{sub ($2,"test.sol.box");}1' test.txt

Here 1 in the end of the block will display all the lines.

FS must be specified in BEGIN block or -F option else first line wont affected.
I have to mention one point here. The above code will replace all the Server.Host instead of specific host line(1.1.1.1).

Regarding your second query that would throw error. cos you are not completed the blocks.

why not you can use sed?
Code:
sed 's/1\.1\.1\.1/test.sol.box/g' test.txt

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 04-18-2012
You cannot change FS after a record is already read. You would change it right away with
Code:
nawk -F'=' 'code...'

or with the BEGIN block


Code:
nawk 'BEGIN{FS=" *= *";OFS=" = ";}$1 == "Server Host" && $2 == "1.1.1.1" {$2="example.com"}1' test.txt

# 4  
Old 04-18-2012
great... the code works.. the ip may vary. so can't go with the sed one liner..

i believe have to try with this

Code:
sed 's/[0-9]\.[0-9]\.[0-9]\.[0-9]/test.sol.box/g'

---------- Post updated at 07:58 AM ---------- Previous update was at 07:55 AM ----------

i got another code from this forum .. i am aware of what it does .. but i could not find the
"variable --> 0" concept. can someone please explain it.

Code:
nawk 'c-->0;/pattern/{print;c=6}'  filename

# 5  
Old 04-18-2012
firstly you match pattern and print that line. you set a counter to 6, so then each line after you evaluate c-- (decrease by 1) and then test if it's above 0. using the semicolon here you omit the action block and it's implied as '{print $0}'. so this will print the following 5 lines as well, since after that, c will become less than zero until the pattern matches again.

so the --> is two operations, not one..
This User Gave Thanks to neutronscott For This Post:
# 6  
Old 04-18-2012
wow.. Smilie now i get it.. i was thinking c-->0 is some kind of pointer stuff.

now i can understand (c--) decrement and " > " comparison.

Thanks you very much Smilie
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 Pattern in file

Ok, so how many times have you received this request? I have been looking through the forum for examples and I see the use of tr, awk and sed to perform similar functions but not sure how to use the tools in this scenario and could use a push in the right direction. GOAL: Search for line... (9 Replies)
Discussion started by: djzah
9 Replies

2. Shell Programming and Scripting

sed find/replace a pattern, but not this one..

I've got a file like so: ...lots of lines, etc. push "route 10.8.0.0 255.255.255.0" push "route 192.168.1.123 255.255.255.0" ...lots of lines, etc. I want to sed find/replace the IP address in the second line, whatever it is, with a new IP address, but I don't want to touch the first line.... (5 Replies)
Discussion started by: DaHai
5 Replies

3. Shell Programming and Scripting

find pattern and replace the text before it

i am editing a big log file with the following pattern: Date: xxxx Updated: name Some log file text here Date: eee Updated: ny Some log file text here Basically i want to remove all the text in a line before the "Updated" pattern. I sill want to print the other... (4 Replies)
Discussion started by: balan1983a
4 Replies

4. Shell Programming and Scripting

Find and Replace pattern in lowercase

I have an xml file. I need to convert a particular pattern to lower case and add 1 to it. For example my sample values in file are: ergeAAwrgersc_DWSTAGE_AC_DBO_TBL_GROUPZONES_INITIAL.badAAergerg sc_DWSTAGE_AC_DBO_TBL_SECTIONDEPENDENCY_INITIAL.badeAAwrgewrg... (6 Replies)
Discussion started by: alfredo123
6 Replies

5. Shell Programming and Scripting

find a pattern and replace

i have a file which contains lines like this. intsrcrpttrn1099mctrl:export GRAPHPARM_AR="-input_code M302023" intsrcrpttrn1099mload:export GRAPHPARM_AR="-input_code M192023" intsrcrpttrn1099mload:export GRAPHPARM_AR="-input_code P192023" the value after -input_code starts with some alphabet... (4 Replies)
Discussion started by: dr46014
4 Replies

6. Shell Programming and Scripting

Find and replace pattern in VI editor

All, I have a text file which has the following data X_SQL_13,X_SQL_14,X_SQL_15,X_SQL_16,X_SQL_17,X_SQL_18,X_SQL_19,X_SQL_20,X_SQL_21,X_SQL_22,X_SQL_23,X_SQL_24,X_SQL_25,X_SQL_26,X_SQL_27,... (4 Replies)
Discussion started by: thana
4 Replies

7. Shell Programming and Scripting

Find a pattern and replace using sed.

Hi I need to help on finding the below pattern using sed <b><a href="/home/document.do?assetkey=x-y-abcde-1&searchclause=photo"> and replace as below in the same line on the index file. <b><a href="/abcde.html"> thx in advance. Mari (5 Replies)
Discussion started by: maridhasan
5 Replies

8. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies

9. Shell Programming and Scripting

find and replace a pattern in a file

Hi I am having 2 files file1.c and file2.c Now i want to find all the occurances of pattern "abc" in file1.c, file2.c and replace with pattern "def" using shell script without using sed and with using sed. Thanks in advance... raju (1 Reply)
Discussion started by: krishnamaraju
1 Replies

10. UNIX for Dummies Questions & Answers

find pattern in FILES and replace it ??

Hi How can I looking for a pattern found in more than one file and replace it with anther pattern this what I was used: find . -name "account.adrs" -depth -follow -exec grep -l "Email = ;" {} \; this print the files name -which is account.adrs- and its path -which is deferent for each... (4 Replies)
Discussion started by: tamer
4 Replies
Login or Register to Ask a Question