21

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

echo "first second third etc"|cut -d " " -f1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hard Rain
  • 1,380
  • 4
  • 14
  • 19
  • Possible duplicate: *[Remove First Word in text stream](https://stackoverflow.com/questions/7814205/remove-first-word-in-text-stream)* – Peter Mortensen Apr 25 '21 at 22:40

5 Answers5

40

You should have a look at info cut, which will explain what f1 means.

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-
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sleepsort
  • 1,321
  • 15
  • 28
18

You can use substring removal for that. There isn't any need for external tools:

$ foo="a b c d"
$ echo "${foo#* }"
b c d
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mat
  • 202,337
  • 40
  • 393
  • 406
7

You can do:

echo "first second third etc" | cut -d " " -f2-
>> second third etc
Aamir
  • 5,324
  • 2
  • 30
  • 47
3

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 Bash tag, you can use Bash parameter expansion like this:

x="first second third etc"
echo ${x#* }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

Try this:

  echo "first second third etc" | cut -d " " -f2-
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331