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
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
Old 07-28-2011
michaelrozar17's Avatar
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...?

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

Sponsored Links
Old 07-28-2011
aigles's Avatar
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 61 Times in 57 Posts

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

Old 07-28-2011
numele numele is offline
Registered User
Join Date: Feb 2010
Last Activity: 11 February 2015, 9:31 PM EST
Posts: 98
Thanks: 19
Thanked 0 Times in 0 Posts
sed '$d' inputfile
Sponsored Links
Old 07-28-2011
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Join Date: Feb 2007
Last Activity: 28 February 2015, 5:36 AM EST
Location: The Netherlands
Posts: 7,727
Thanks: 128
Thanked 543 Times in 511 Posts
The awk solutions above leave a field separator after each line.
You can avoid that with something like:

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

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 09:03 PM
AWK : Add Fields of lines with matching field DerSeb Shell Programming and Scripting 6 01-16-2011 09:41 PM
Sorting on two fields time field and number field pat4519 Shell Programming and Scripting 2 01-08-2010 03: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 09:04 AM
How to split a field into two fields? vbrown Shell Programming and Scripting 4 02-21-2008 06:50 AM

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