Free tech support / small logo


Chopping off the last field of each line?

I'm working on the Web site for Creating Cool Web Sites and just realized that the little Unix trick I used while editing a file is actually a beautiful example of why so many people love Unix so much, so I thought I'd share it. The problem: a file where lines have 1-10 words + a last field value (in this case, a page number) that I don't want. The challenge is to figure out how to easily remove that last field, when there are a variable number of fields on the line.

Dave's Answer: My first instinct was to turn to awk or perl, but in fact there's a much easier way using basic Unix utilities: rev and cut:
$ rev inputfile | cut -f2- | rev > outputfile
How does this work? The rev command reverses each line of input, so that means that the first field of each now-reversed line is the field that we want to remove. That's easily done with cut with the -f2- flag (that means output field two through the end of the line). Then, finally, we re-reverse (that is, fix) each line and save the output in a new file.

Quickly and easily done.





Categorized: Unix and Linux Help   (Article 3690, Written by )
Tagged:
Previous: How do I tweak my PATH?
Next: Another nice review of Wicked Cool Shell Scripts




Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

well done

Posted by: pedestrian at August 19, 2007 9:20 AM

try this

awk '{$NF=""; print $0}'

Cheers
Amit

Posted by: Amit at July 7, 2008 12:28 AM

I prefer:

awk '{NF--;print}'

With NF="" you will have additional spaces at the end of each line.

Posted by: porges at March 5, 2009 7:50 AM

Nice, this just solved a problem I've been working on. So thanks.

I figured I'd try to make it work without the rev. Reversing the argument of the -f parameter should returned the same results.

Posted by: Cannon at February 13, 2010 2:07 PM

awk '{NF--;print}' Prints the whole name for me. What am I missing?

awk '{NF="";print}' works, but I need to include the periods.

Posted by: Rywin at March 25, 2010 9:22 AM

I had to strip the last field from some 2Million line apache logs. First I tried the reverse-cut-reverse approach and that was likely to take 'forever'.
So I found a solution with sed that worked a lot better:
date; time (gzcat infile.gz | sed 's/\(.*\)\ \(.*\)/\1/' | gzip -c - > outfile.gz ); date
What the sed command does is strip 1 parameter to the preceeding space from the end of the line.

Posted by: Sean Meacher at January 4, 2011 7:39 AM

I managed to do this with awk:

cat filename |awk -F/ '{gsub($NF,"");print}'

The gsub replaces the last field with a null string. My delimiter is /

Nick

Posted by: Nick at January 17, 2011 9:15 AM

Thanks Nick..


awk -F/ '{gsub($NF,"");print}'

helped me too much.. was searching for this..

I am adding something else

awk -F/ '{gsub($NF,"");sub(".$", "");print}'

to cut the last part with delimiter as /

input
=====
/home/smilyface/aaa/bbb
output
======
/home/smilyface/aaa

Posted by: smilyface at July 30, 2011 7:09 AM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!

Linux Journal: Free Issue!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.