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,030
  • 21
  • 100
  • 124
Hard Rain
  • 1,280
  • 3
  • 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

39

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,030
  • 21
  • 100
  • 124
sleepsort
  • 1,301
  • 13
  • 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,030
  • 21
  • 100
  • 124
Mat
  • 195,986
  • 40
  • 382
  • 396
7

You can do:

echo "first second third etc" | cut -d " " -f2-
>> second third etc
Aamir
  • 5,218
  • 2
  • 28
  • 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,030
  • 21
  • 100
  • 124
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
1

Try this:

  echo "first second third etc" | cut -d " " -f2-
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Rahul Tripathi
  • 161,154
  • 30
  • 262
  • 319