[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 807
  • Last Modified:

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.
0
mechanicus01
Asked:
mechanicus01
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
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
 
woolmilkporcCommented:
cut -f2- -d" " filename

wmp
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
farzanjCommented:
sed "s/^[A-Za-z0-9_-]*//" filename
0
 
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:
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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now