Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Help
  • Chopping off the last field of each line?

Chopping off the last field of each line?

April 5, 2004 / Dave Taylor / Linux Help / 11 Comments

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.

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.

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
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, Dave, for all your helpful information by buying you a cup of coffee!

11 comments on “Chopping off the last field of each line?”

  1. Ice says:
    November 15, 2012 at 5:07 pm

    Thanks a lot!

    Reply
  2. smilyface says:
    July 30, 2011 at 7:09 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

    Reply
    • TB or not TB says:
      April 4, 2014 at 2:57 pm

      This was exactly what I was looking for…thanks.

      You could shorten second command to:
      awk -F/ ‘{sub(“/”$NF,””);print}’

      Be careful copy/pasting from this website…the quotes don’t match unix quotes…had to overwrite single/double quotes.

      Reply
      • Dave Taylor says:
        April 5, 2014 at 8:02 am

        Good point about the quotes, TB. Don’t know how to tweak it to have “straight” quotes in the theme or I’d so so. 🙂

        Reply
  3. Nick says:
    January 17, 2011 at 9:15 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

    Reply
  4. Sean Meacher says:
    January 4, 2011 at 7:39 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.

    Reply
  5. Rywin says:
    March 25, 2010 at 9:22 am

    awk ‘{NF–;print}’ Prints the whole name for me. What am I missing?
    awk ‘{NF=””;print}’ works, but I need to include the periods.

    Reply
  6. Cannon says:
    February 13, 2010 at 2:07 pm

    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.

    Reply
  7. porges says:
    March 5, 2009 at 7:50 am

    I prefer:
    awk ‘{NF–;print}’
    With NF=”” you will have additional spaces at the end of each line.

    Reply
  8. Amit says:
    July 7, 2008 at 12:28 am

    try this
    awk ‘{$NF=””; print $0}’
    Cheers
    Amit

    Reply
  9. pedestrian says:
    August 19, 2007 at 9:20 am

    well done

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • Can I Remotely Disable Apple Pay on my iPhone?
  • Easy Way to Check for a System Update on KOBO Libra H2O eReader?
  • Disable Contactless Google Pay (GPay) on an Android Phone?
  • How to Have a Honda Vehicle Forget Your Phone?
  • How Do I Free Up Disk Space On My Windows PC?

On Our YouTube Channel

Monoprice Harmony Capsule 300 Outdoor Bluetooth Speaker -- UNBOXING, DEMO & REVIEW

Twinkly "DOTS" Flexible Smart LED String -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (194)
  • Apple iPad Help (144)
  • Apple Watch Help (51)
  • Articles, Tutorials, and Reviews (344)
  • Business Advice (198)
  • Chrome OS Help (21)
  • Computer & Internet Basics (756)
  • d) None of the Above (164)
  • Facebook Help (381)
  • Google, Chrome & Gmail Help (175)
  • HTML & Web Page Design (244)
  • Instagram Help (47)
  • iPhone & iOS Help (602)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (92)
  • LinkedIn Help (85)
  • Linux Help (162)
  • Linux Shell Script Programming (86)
  • Mac & MacOS Help (887)
  • Most Popular (16)
  • Outlook & Office 365 Help (24)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (17)
  • SEO & Marketing (81)
  • Spam, Scams & Security (92)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (909)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (45)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (56)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


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 site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy