Sunday, February 24, 2008

HowTo: Strip Out First Word of Line from Text Files

This is a quick post on how to remove the first word of a line.

There are lot of ways on how to remove and strip out the first word of a line from a text file. Here's one way to achieve that.

# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my blog name is sysad linux blog
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Assuming we need to strip down or remove the word 'my' from the above line, that would be basically easy using linux binary tools. One approach would be using awk like so

Here's how to remove the first word of a line from textfiles using static approach via awk and cut linux tool.

Working on same content of previous file
# cat testfile.txt | awk '{print $2,$3,$4,$5,$6,$7}'
output
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
blog name is sysad linux blog
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Using cut, that would be

# cat testfile.txt | cut -d' ' -f2,3,4,5,6,7

That's it, enjoy.

Related Articles:
Get the Filename Extension Only
Strip Out Directory Path and Filename Suffixes
Removing New Lines and Return Lines from Text Files
Have you ever head of TR in Linux?
Inverting or Reversing Grep Matches
Strip Out Non-Directory Path from Filename
Removing Spaces from Filenames
Removing Blank Lines using Sed Or Grep
50 Quick Linux Command Tips Part 4

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy