7

consider a file names 'file.txt'. It contains the following.

dove is a bird
tiger is an animal
cricket is a game.

Expected output:

is a bird
is an animal
is a game.
1
  • Delete the first word and the trailing space...
    – Jeff Schaller
    Commented Apr 24, 2019 at 14:08

8 Answers 8

19

To do it using cut

cut -f 2- -d ' ' file.txt > new_file.txt

"Give me the second and any other field beyond, using space as a delimiter, from the file.txt file and direct the output to new_file.txt"

6

using sed :

sed 's/[^ ]* //' list.txt
  • will remove all till first space
2
  • 1
    ... and through the first space :)
    – Jeff Schaller
    Commented Apr 24, 2019 at 14:24
  • @JeffSchaller yes....
    – Siva
    Commented Apr 24, 2019 at 14:26
5

with awk:

awk '{ $1=""; print substr($0,2) }' file.txt
3

With sed

To remove the first word: sed -E "s,^[[:alnum:]]+ ,," list.txt

To remove the first character: sed -E "s,^[[:alnum:]],," list.txt

1
  • what if it has any punctuations or symbols...
    – Siva
    Commented Apr 24, 2019 at 14:40
1

In Perl:

perl -pe 's/.*? //'  input

In Grep:

grep -Po ' \K.*' input
1

Use ed, man!

ed -s input <<< $'%s/^[^ ][^ ]* //\nw\nq'

or, with a here-string:

printf '%s\n' '%s/^[^ ][^ ]* //' 'w' 'q' | ed -s input

This sends three newline-separated commands to ed:

  1. on every (%) line, search and replace one or more non-space characters and a trailing space with nothing; the search pattern is anchored to match at the beginning of the line with ^
  2. write the file back to disk
  3. quit
0

awk is best, because it also removed preceding whitespace. And it's aliasable:

alias removefirstword="awk '{ \$1=\"\"; print substr(\$0,2) }'"

testhost: uptime

15:26:50 up 0 days, 14:42, 1 users, load average: 1.24, 1.28, 1.33

testhost: uptime | removefirstword

up 0 days, 14:42, 1 users, load average: 1.19, 1.26, 1.32

Of course, the other regexp solutions could do this, too.

-1

With bash:

while read; do
  set -f -- $REPLY
  shift
  echo "$@"
done < infile

Output:

is a bird
is an animal
is a game.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .