Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

awk - print all fields except for last field

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 07-28-2011
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 24 November 2014, 9:59 PM EST
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
This one helps...?

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

Sponsored Links
    #3  
Old Unix and Linux 07-28-2011
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts

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

Jean-Pierre.
    #4  
Old Unix and Linux 07-28-2011
numele numele is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 14 April 2015, 3:02 PM EDT
Posts: 98
Thanks: 19
Thanked 0 Times in 0 Posts
sed '$d' inputfile
Sponsored Links
    #5  
Old Unix and Linux 07-28-2011
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 17 April 2015, 11:07 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 128
Thanked 544 Times in 512 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 12:52 AM.