How to print 1st field and last 2 fields together and the rest of the fields after it using awk? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-25-2013
100bees 100bees is offline
Registered User
 
Join Date: May 2013
Last Activity: 29 August 2013, 8:23 AM EDT
Location: Chennai,India
Posts: 16
Thanks: 10
Thanked 0 Times in 0 Posts
How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts,

I need to print the first field first then last two fields should come next and then i need to print rest of the fields.

Input :

Code:
a1,abc,jsd,fhf,fkk,b1,b2
a2,acb,dfg,ghj,b3,c4
a3,djf,wdjg,fkg,dff,ggk,d4,d5

Expected output:

Code:
a1,b1,b2,abc,jsd,fhf,fkk
a2,b3,c4,acb,dfg,ghj
a3,d4,d5,djf,wdjg,fkg,dff,ggk


I got how to print the first and last two fields together ,but not sure how to print the rest of the fields after this


Code:
 
awk -F ',' '{
print $1,$(NF-1),$NF}' infile

Sponsored Links
    #2  
Old 06-25-2013
rajamadhavan rajamadhavan is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 11 August 2014, 2:25 AM EDT
Location: Bangalore, India
Posts: 279
Thanks: 4
Thanked 58 Times in 57 Posts

Code:
$ awk -F"," '{i=$(NF-1);j=$NF;for(k=NF;k>=4;k--){$k=$(k-2);};$2=i;$3=j} {print $0}' aak
a1 b1 b2 abc jsd fhf fkk
a2 b3 c4 acb dfg ghj
a3 d4 d5 djf wdjg fkg dff ggk


Last edited by rajamadhavan; 06-25-2013 at 05:53 AM..
The Following User Says Thank You to rajamadhavan For This Useful Post:
100bees (06-25-2013)
Sponsored Links
    #3  
Old 06-25-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts

Code:
awk -F, '{A=$1","$(NF-1)","$NF;sub($(NF-1)","$NF,"");$1=A}1' OFS="," file


Last edited by pamu; 06-25-2013 at 05:20 AM.. Reason: corrected
The Following User Says Thank You to pamu For This Useful Post:
100bees (06-25-2013)
    #4  
Old 06-25-2013
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 21 November 2014, 4:45 AM EST
Location: Bangalore,India
Posts: 2,091
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
$ sed "s/\([^,]*\)\(.*\)\(,[^,]*,.*\)$/\1\3\2/" file
a1,b1,b2,abc,jsd,fhf,fkk
a2,b3,c4,acb,dfg,ghj
a3,d4,d5,djf,wdjg,fkg,dff,ggk

The Following User Says Thank You to anbu23 For This Useful Post:
100bees (06-25-2013)
Sponsored Links
    #5  
Old 06-25-2013
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 28 November 2014, 6:46 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 123
Thanked 538 Times in 508 Posts
Another approach:

Code:
awk -F, '{$1=$1 FS $(NF-1) FS $NF}NF=NF-2' OFS=, file

The Following User Says Thank You to Franklin52 For This Useful Post:
100bees (06-25-2013)
Sponsored Links
    #6  
Old 06-25-2013
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 27 November 2014, 1:23 AM EST
Location: Beijing China
Posts: 1,294
Thanks: 0
Thanked 24 Times in 24 Posts
python


Code:
with open("a.txt") as f:
 for line in f:
  line=line.replace("\n","")
  words=line.split(",")
  words=[words[0]]+words[len(words)-2:]+words[1:len(words)-2]
  print(",".join(words))


Last edited by Franklin52; 06-25-2013 at 06:00 AM.. Reason: Code tags
The Following User Says Thank You to summer_cherry For This Useful Post:
100bees (06-26-2013)
Sponsored Links
    #7  
Old 06-25-2013
100bees 100bees is offline
Registered User
 
Join Date: May 2013
Last Activity: 29 August 2013, 8:23 AM EDT
Location: Chennai,India
Posts: 16
Thanks: 10
Thanked 0 Times in 0 Posts
Thank you all for your valuable inputs it helped me.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to split one field and print the last two fields within the split part. yifangt Shell Programming and Scripting 5 04-17-2012 05:41 AM
How to Print from nth field to mth fields using awk machomaddy Shell Programming and Scripting 8 03-02-2012 10:41 AM
awk reverse first two fields on match and print rest of line sdf Shell Programming and Scripting 5 11-08-2011 03:15 AM
awk - print all fields except for last field locoroco Shell Programming and Scripting 4 07-28-2011 10:48 AM
Compare Tab Separated Field with AWK to all and print lines of unique fields. rocket_dog Shell Programming and Scripting 1 05-26-2011 09:03 PM



All times are GMT -4. The time now is 11:28 PM.