Remove First Word in text stream

2019-02-02 05:06发布

问题:

How would I remove the first word from each line of text in a stream? i.e.

$cat myfile 
some text 1
some text 2
some text 3

what I want is

$cat myfile | magiccommand 
text 1
text 2
text 3

How would I go about this using bash? I could use awk '{print $2 $3 $4 $5 ....}' but that's messy and would result in extra spaces for all null arguments. I was thinking that sed might be able to do this, but I could not find any examples of this. Any help is appreciated! Thanks!

回答1:

based on your example text,

cut -d' ' -f2- yourFile

should do the job.



回答2:

That should work:

$ cat test.txt
some text 1
some text 2
some text 3

$ sed -e 's/^\w*\ *//' test.txt
text 1
text 2
text 3


回答3:

Here is a solution using awk

awk '{$1= ""; print $0}' yourfile 


回答4:

run this sed "s/^some\s//g" myfile you even don't need to use a pipe



回答5:

To remove the first word, until space no matter how many spaces exist, use: sed 's/[^ ]* *//'

Example:

$ cat myfile 
some text 1
some  text 2
some     text 3

$ cat myfile | sed 's/[^ ]* *//'
text 1
text 2
text 3


标签: bash sed awk cat