0

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.
  • Delete the first word and the trailing space... – Jeff Schaller Apr 24 at 14:08
5

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"

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

  • what if it has any punctuations or symbols... – msp9011 Apr 24 at 14:40
3

using sed :

sed 's/[^ ]* //' list.txt
  • will remove all till first space
  • 1
    ... and through the first space :) – Jeff Schaller Apr 24 at 14:24
  • @JeffSchaller yes.... – msp9011 Apr 24 at 14:26
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

with awk:

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

With bash:

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

Output:

is a bird
is an animal
is a game.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.