How to remove the first part of a string in bash?

2019-03-18 03:38发布

This code will give the first part, but how to remove it, and get the whole string without the first part?

echo "first second third etc"|cut -d " " -f1

标签: bash shell
5条回答
姐就是有狂的资本
2楼-- · 2019-03-18 04:21

Try this:-

  echo "first second third etc"|cut -d " " -f2-
查看更多
我命由我不由天
3楼-- · 2019-03-18 04:22

You can do:

echo "first second third etc" | cut -d " " -f2-
>> second third etc
查看更多
太酷不给撩
4楼-- · 2019-03-18 04:29

You can use substring removal for that, no need for external tools:

$ foo="a b c d"
$ echo "${foo#* }"
b c d
查看更多
Animai°情兽
5楼-- · 2019-03-18 04:30

You should have a look at info cut, which will explain what f1 means. Also, a same question here: question-7814205

Actually we just need fields after(and) the second field. -f tells the command to search by field, and 2- means the second and following fields.

echo "first second third etc" | cut -d " " -f2-
查看更多
贼婆χ
6楼-- · 2019-03-18 04:38

Try doing this :

echo "first second third etc"|cut -d " " -f2-

It's explained in

 man cut | less +/N-

N- from N'th byte, character or field, to end of line

As far of you have the tag, you can use bash parameter expansion like this :

x="first second third etc"
echo ${x#* }
查看更多
登录 后发表回答