LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-17-2009, 02:56 AM   #1
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
sed replcement


I have a line like
127.0.0.1 abc def hij klm nop qrs

I want a sed/awk expression which gives me output like (I want the list corresponding to IP to start with nop)
127.0.0.1 nop abc def hij klm qrs
 
Old 07-17-2009, 03:01 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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.
 
Old 07-17-2009, 03:03 AM   #3
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
No colucix, the nop is not in the same position and the number of fields are not same as well an there could be an entry like nop.amr.com

Sorry for not explainig the question
 
Old 07-17-2009, 03:08 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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)?
 
Old 07-17-2009, 03:12 AM   #5
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
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.
 
Old 07-17-2009, 03:20 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
You can try this in awk:
Code:
/nop/{
 for ( i=2; i<=NF; i++ )
   if ( $i ~ "nop" )
     str1=$i
   else
     str2=(str2 " " $i)
 print $1, str1, substr(str2,2)
}
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.
 
Old 07-17-2009, 04:15 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk '{t=$(NF-1); o=$1;$1=$(NF-1)=""; print o,t,$0}' file
 
Old 07-17-2009, 05:23 AM   #8
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
Hey ghostdog74, colucix

This code in not working
the file is
Code:
192.9.200.1     nop
192.9.200.2     nop_try
127.0.0.1       abc yop tun tre nop nop.xyz.com gre
127.0.0.7       rte
and from awk I am trying
Code:
 awk '{ if ($0 ~ /^127.0.0.1/) { x=""; for (i = 2; i<=NF; i++) { if ($i != "nop"){ x="$x" "$i"}}; printf("%s %s\n","127.0.0.1","nop",$x)} else {print}}' test.txt
But this is not working could you please help

I want the entry against 127.0.0.1 to start with nop
 
Old 07-17-2009, 05:29 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
so what is your final output you want?
 
Old 07-17-2009, 05:29 AM   #10
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
This is the final output I want

Code:
192.9.200.1     nop
192.9.200.2     nop_try
127.0.0.1       nop abc yop tun tre nop.xyz.com gre
127.0.0.7       rte

Last edited by PMP; 07-17-2009 at 05:42 AM.
 
Old 07-17-2009, 06:19 AM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Code:
{ 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.
 
Old 07-17-2009, 06:20 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk '/127.0.0.1/{
 for(i=1;i<=NF;i++){
    if($i=="nop"){t=$i; $(i)="" }
 }
 o=$1
 $1=""
 print o,t,$0
 next
}1' file
 
  


Reply

Tags
expression, regular, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed help monkeynuts84 Linux - General 3 03-09-2007 12:39 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 09:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration