awk to substitute ip without zero left padding


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to substitute ip without zero left padding
# 1  
Old 12-30-2012
awk to substitute ip without zero left padding

Hello All,

I have this script to awk IP to new file.

Code:
#awk '/myip|yourip/ {sub(/...\....\....\..../, newip)}1' newip=$IP  existing.txt > new.txt

When existing.txt has myip=192.168.123.123 and $IP has 192.168.12.12, the awk script is not working. But while I add zero left padding to $IP i.e, 192.168.012.012 it works.

Same the case when myip=192.168.12.12 and $IP has 192.168.123.123, does not work.

Inshort, the IP format need to be 3 digit decimal to work. Any suggestions.

Regards
Shaan

Last edited by Scrutinizer; 12-31-2012 at 03:58 AM.. Reason: code tags
# 2  
Old 12-31-2012
I don't know whats the problem is.Smilie

because it works perfect here..
Code:
$ cat file
myip=192.168.123.123

$ awk '/myip|yourip/{sub(/...\....\....\..../,newip)}1' newip="192.168.12.12" file
myip=192.168.12.12

could you please provide your sample input.
This User Gave Thanks to pamu For This Post:
# 3  
Old 12-31-2012
Try /.+\..+\..+\..+/ instead of /...\....\....\..../, or:
Code:
awk -F= '/myip|yourip/{sub($2, newip)}1' newip=$IP infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-31-2012
Thanks. /.+\..+\..+\..+/ instead of /...\....\....\..../ works, but that does not copy other text from existing.txt. it only captures IP.

But somehow, my existing code started working as Pamu noticed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Left pad spaces using awk or sed

Hi,I've a unix pipe delimited file as below f1|f2|f3|f4|f5|f6 My requirement is to pad spaces on the left to fields f2, f3 and f5. Field Lengths according to file layout f2 - 4 char f3 - 5 char f5 - 3 char If my record is as below 1|43|bc|h0|34|a Output record should be as below 1| 43| bc|h0|... (4 Replies)
Discussion started by: Soujanya_K
4 Replies

2. Shell Programming and Scripting

left join using awk

Hi guys, I need AWK to merge the following 2 files: file1 1 a 1 1 2 b 2 2 3 c 3 3 4 d 4 4 file2 a a/a c/c a/c c/c a/a c/t c c/t c/c a/t g/g c/c c/t desired output: 1 a 1 1 a/a c/c a/c c/c a/a c/t 2 b 2 2 x x x x x x 3 c 3 3 c/t c/c a/t g/g c/c c/t 4 d 4 4 x x x x x x (2 Replies)
Discussion started by: g1org1o
2 Replies

3. Shell Programming and Scripting

left join using awk

Hi guys, I need to use awk to join 2 files file_1 A 001 B 002 C 003 file_2 A XX1 B XX2 output desired A 001 XX1 B 002 missing C 003 XX2 thank you! (2 Replies)
Discussion started by: g1org1o
2 Replies

4. Shell Programming and Scripting

substitute with awk

When the line contains abc, it will goes to the next line and substitue the MM to NN bc 23 33 abc 23 33 ddd MM xx dff MM 33 cat xxx |awk '{if ($0~/abc/){getline;sub(/MM/,"NN")}{print}}', It doesn't show "abc 23 33 bc 23 33 ddd NN xx dff MM 33 bc 23 33 abc 23 33 ddd NN xx... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

5. Shell Programming and Scripting

Left padding in Unix

I am passing input string,length, and the pad character. input string=123 Pad char=# Length=6 then the output should be: ###123 How we can do this? Thanks (5 Replies)
Discussion started by: pandeesh
5 Replies

6. Shell Programming and Scripting

awk padding column

Hello, i am trying to pad a column A1 A2 A10 B2 B8 B12 to look like A01 A02 A10 B02 B08 B12 using awk (6 Replies)
Discussion started by: mykey242
6 Replies

7. Shell Programming and Scripting

Unix Cut or Awk from 'Right TO Left'

Hello, I want to get the User Name details of a user from a file list. This list can be in the format: FirstName_MiddleName1_LastName_ID FirstName_LastName_ID FirstName_MiddleName1_MiddleName2_LastName_ID What i want it to return is FirstName_MiddleName1_LastName of a user. I... (6 Replies)
Discussion started by: limamichelle
6 Replies

8. Shell Programming and Scripting

Left join on files using awk

nawk 'NR==FNR{a;next} {if($1 in a) print $1,"Found" else print}' OFS="," File_B File_A The above code is not working help is appreciated (6 Replies)
Discussion started by: pinnacle
6 Replies

9. UNIX for Dummies Questions & Answers

AWK and padding values

Hi, I am trying to parse a file of fixed length transactions into a new file. Using awk I need to add the line number as a 5 digit long numeric value to each of the transactional rows. i.e. awk '{print <ROW NUM PADDED HERE>substr($0,1,8).........}' filename > newfile I know that NR-1... (3 Replies)
Discussion started by: kshelluser
3 Replies

10. UNIX for Dummies Questions & Answers

left padding numbers

Hi, can someone please tell me how to left-pad numbers using unix. e.g. 1234 -> 00001234 Thanks in advance for your help. (1 Reply)
Discussion started by: colquhoi
1 Replies
Login or Register to Ask a Question