ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If the "nop" field always appears in the same position and the number of fields is always the same, you can simply print out the fields in the order you want using awk.
Is the entry nop.amr.com always the same or it just contains the string "nop"? In other words you want to match the whole field or just a part of it (the rest is unknown)?
it contains the string nop, i want to take out the exact match of nop. nop could be at beginning as well, got stuck tried so many sed combination but didnt work. At last posted here.
It stores the fields containing "nop" in str1 and the rest in str2. The substr statement is to mask the first character that is an unwanted blank space. I assume the IP is always the first field.
{ start = index($0, $2)
format = ("%-" start-2 "s %s %s\n")
}
/^127.0.0.1/{
for ( i=2; i<=NF; i++ )
if ( $i == "nop" )
str1=$i
else
str2=(str2 " " $i)
printf format, $1, str1, substr(str2,2)
}
! /^127.0.0.1/
The first statement get the position of the second field and build the format string accordingly. This will preserve the format of the input file (with the correct number of spaces after the IP address). The last statement will print out the lines not starting with 127.0.0.1. Hope this is what you're looking for.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.