Hard Rain
Hard Rain

Reputation: 1410

How can I remove the first part of a string in Bash?

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

Upvotes: 20

Views: 41954

Answers (5)

Mat
Mat

Reputation: 206709

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

Upvotes: 19

Rahul Tripathi
Rahul Tripathi

Reputation: 172448

Try this:

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

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185161

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#* }

Upvotes: 3

sleepsort
sleepsort

Reputation: 1331

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-

Upvotes: 40

Aamir
Aamir

Reputation: 5440

You can do:

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

Upvotes: 7

Related Questions