awk - print all fields except for last field | 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.

awk - print all fields except for last field

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-28-2011
locoroco locoroco is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 15 February 2014, 7:50 PM EST
Posts: 127
Thanks: 44
Thanked 3 Times in 3 Posts
awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field?
Sponsored Links
    #2  
Old 07-28-2011
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
This one helps...?

Code:
awk '{$NF=""}1' inputifile

Sponsored Links
    #3  
Old 07-28-2011
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 20 January 2014, 5:51 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts

Code:
awk '{$(NF--)=""; print}' inputfile

Jean-Pierre.
    #4  
Old 07-28-2011
numele numele is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 4 September 2014, 3:12 PM EDT
Posts: 98
Thanks: 19
Thanked 0 Times in 0 Posts
sed '$d' inputfile
Sponsored Links
    #5  
Old 07-28-2011
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 18 September 2014, 10:40 AM EDT
Location: The Netherlands
Posts: 7,716
Thanks: 123
Thanked 537 Times in 507 Posts
The awk solutions above leave a field separator after each line.
You can avoid that with something like:

Code:
awk 'sub(FS $NF,x)' file

Quote:
Originally Posted by numele View Post
sed '$d' inputfile
This removes the last line.
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
Compare Tab Separated Field with AWK to all and print lines of unique fields. rocket_dog Shell Programming and Scripting 1 05-26-2011 08:03 PM
AWK : Add Fields of lines with matching field DerSeb Shell Programming and Scripting 6 01-16-2011 08:41 PM
Sorting on two fields time field and number field pat4519 Shell Programming and Scripting 2 01-08-2010 02:45 PM
Get 4 character each from 2 different fields concatenate and add as a new field ajithshankar@ho Shell Programming and Scripting 4 08-07-2009 08:04 AM
How to split a field into two fields? vbrown Shell Programming and Scripting 4 02-21-2008 05:50 AM



All times are GMT -4. The time now is 11:50 AM.