awk: process a part of string, leaving others unchanged


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: process a part of string, leaving others unchanged
# 1  
Old 09-26-2012
awk: process a part of string, leaving others unchanged

Hello,

I have a bunch of strings of the following format:

Code:
11.22.33.44.in-addr.arpa x.y.z.

What I want to do is format each string in this way:

Code:
44.33.22.11 x.y.z

i.e. the string conists of two columns, where delimiter is space. I need to output modified first column (delimiter inside that column is . ), leaving the second unchanged.

The first part is somewhat straitforward:
Code:
awk -F'.' '{ print $4"."$3"."$2"."$1 }'

I've got problem with outputting the second column, appended to the result of the aforementioned expression.

Or maybe there's a simpler way to do this without using awk?

Last edited by Scrutinizer; 09-26-2012 at 09:48 AM.. Reason: code tags
# 2  
Old 09-26-2012
Code:
echo '11.22.33.44.in-addr.arpa x.y.z'|awk -F'[. ]' '{print $4,$3,$2,$1 " " $(NF-2),$(NF-1),$NF}' OFS='.'

Please post more of your input lines/strings.
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 09-26-2012
Quote:
Originally Posted by elixir_sinari
Code:
echo '11.22.33.44.in-addr.arpa x.y.z'|awk -F'[. ]' '{print $4,$3,$2,$1 " " $(NF-2),$(NF-1),$NF}' OFS='.'

Please post more of your input lines/strings.
elixir_sinari

Nice example, but that's will work only for 3 words, delimited by . in second column.

I forgot to mention, but there are variable number of words, delimited by dot in right column.

E.g:

Code:
11.22.33.44.in-addr.arpa xxx.yyy
22.33.44.55.in-addr.arpa xxx.yyy.zzz
100.5.10.20.in-addr.arpa a.b.c.d


Last edited by Scrutinizer; 09-26-2012 at 09:48 AM.. Reason: code tags
# 4  
Old 09-26-2012
This will work for your sample data:
Code:
sed 's/^\([^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)\./\4.\3.\2.\1 /;s/ .* / /' file

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 09-26-2012
MySQL

Quote:
Originally Posted by elixir_sinari
This will work for your sample data:
Code:
sed 's/^\([^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)\./\4.\3.\2.\1 /;s/ .* / /' file

wow, that actually works, thank you!

The syntax is a bit obscure, so I'm going to sort this out using some sed documentation.
# 6  
Old 09-26-2012
try this...
Code:
awk '{ for (i=1;i<=NF;i++){if($i ~ /[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.+/){ split($i,a,".");printf a[1]"."a[2]"."a[3]"."a[4]" "}else{ if(i!=NF){printf $i" "}else{printf $i"\n"}}}}' OFS="." file

This User Gave Thanks to pamu For This Post:
# 7  
Old 09-26-2012
Something simpler with awk:
Code:
awk '{split($1,a,".");print a[4],a[3],a[2],a[1] " " $2}' OFS='.' file

This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove mutiple values from specific pattern, leaving a single value

In the awk below I am trying to remove all instances after a ; (semi-colon) or , (comma) in the ANN= pattern. I am using gsub to substitute an empty string in these, so that ANN= is a single value (with only one value in it the one right after the ANN=). Thank you :). I have comented my awk and... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

awk - how to compare part of the string?

Need help for awk.. file will have comma separated numbers, I need check digits before 10 numbers eg ( 001)1234567890 Basically want to check country code of a mobile number. eg: abc,def,data, data,0011234567890, data,data Script should be checking country code with 001, I will pass... (10 Replies)
Discussion started by: vegasluxor
10 Replies

3. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

4. Shell Programming and Scripting

Need Awk command to get part of string based on delimeter

HI, Need awk command to get date and time alone from Input : "15:29:15 28.08.2010|SCHEDULE: Started program POSG1" Output expected : "15:29:15 28.08.2010" Please help. (9 Replies)
Discussion started by: shanneykar
9 Replies

5. Shell Programming and Scripting

How to get part of string in awk from match

Hi, Im an awk noob and I am having trouble trying to get matches. Here is my script: #!/bin/gawk -f BEGIN {} $0 ~ /<a href=".*">.*<\/a>/{print} Ideally I want to be able to get the actual link and print it. In PHP you can do preg_replace and get the match you want by using \\1 where 1... (2 Replies)
Discussion started by: adsyuk
2 Replies

6. Shell Programming and Scripting

sed/awk: Delete matching words leaving only the first instance

I have an input text that looks like this (comes already sorted): on Caturday 22 at 10:15, some event on Caturday 22 at 10:15, some other event on Caturday 22 at 21:30, even more events on Funday 23 at 11:00, yet another event I need to delete all the matching words between the lines, from... (2 Replies)
Discussion started by: GrinningArmor
2 Replies

7. Shell Programming and Scripting

Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any... (4 Replies)
Discussion started by: stakuri
4 Replies

8. UNIX for Dummies Questions & Answers

delete string in a text file leaving the first occurrence

Hi, How can i delete the second and subsequent occurrence of a particular string from a file ? eg) test.txt cattle bat battle mat matter cattle cattle my output file should be cattle bat battle mat matter (12 Replies)
Discussion started by: gopskrish
12 Replies

9. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies
Login or Register to Ask a Question