Remove First Word From Each Line in File

Hello
I need a shell script to remove the first word from each line in a text file.
The file looks like this:

KERB5_TAG-43 KERB5 SET REALM ""
KERB5_TAG KERB5 SET SRVTAB ""

Open in new window


I am looking to remove the KERB5_TAG-43 and KERB5_TAG.
LVL 1
mechanicus01Asked:
Who is Participating?
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Or simpler using cut:
cat <filename> | cut -d" " -f2-

Open in new window

Explanation: cut lines with space as a separator and print field(s) 2 and further, fill in your file name at <filename> (without the < > characters).
0
 
farzanjCommented:
sed "s/^[A-Za-z0-9_]*//" filename

Use -i if you want to make changes to the file
0
 
mechanicus01Author Commented:
Some of the lines have a "-" followed by a number.
Ex:
LDAP-TAG-92
LDAP-TAG-93

Open in new window



0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
woolmilkporcCommented:
cut -f2- -d" " filename

wmp
0
 
farzanjCommented:
sed "s/^[A-Za-z0-9_-]*//" filename
0
 
farzanjCommented:
With sed you can also change the file in one go
sed -i "s/^[A-Za-z0-9_-]*//" filename

Cut would be too sensitive about the number of spaces as delimiters and would simply break if there are two spaces instead of one and you cannot change the file without redirection.

awk would be a better substitute of cut
0
 
mechanicus01Author Commented:
OK, thanks...Let me figure out how to distribute the points.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
@farzani: cut will work just fine, as the OP says: "The file looks like this" - no way telling that the your sed pattern A-Za-z0-9_- will work in all cases.

Both cut and sed work perfectly for the sample given. awk would be more flexible in recognizing whitespace, tabs etc. We could ask for more details about the source file instead of making assumpitons ;)
0
 
mechanicus01Author Commented:
Thanks.. they all worked but the cleanest using cut. Sed with regex worked but not all lines..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.