LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed replcement (https://www.linuxquestions.org/questions/programming-9/sed-replcement-740697/)

PMP 07-17-2009 02:56 AM

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

colucix 07-17-2009 03:01 AM

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.

PMP 07-17-2009 03:03 AM

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

colucix 07-17-2009 03:08 AM

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)?

PMP 07-17-2009 03:12 AM

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.

colucix 07-17-2009 03:20 AM

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.

ghostdog74 07-17-2009 04:15 AM

Code:

awk '{t=$(NF-1); o=$1;$1=$(NF-1)=""; print o,t,$0}' file

PMP 07-17-2009 05:23 AM

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

ghostdog74 07-17-2009 05:29 AM

so what is your final output you want?

PMP 07-17-2009 05:29 AM

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


colucix 07-17-2009 06:19 AM

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.

ghostdog74 07-17-2009 06:20 AM

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



All times are GMT -5. The time now is 05:12 PM.