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.
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"
using sed :
sed 's/[^ ]* //' list.txt
With sed
To remove the first word: sed -E "s,^[[:alnum:]]+ ,," list.txt
To remove the first character: sed -E "s,^[[:alnum:]],," list.txt
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
:
%
) line, s
earch 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 ^
w
rite the file back to diskq
uitawk 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.
With bash
:
while read; do
set -f -- $REPLY
shift
echo "$@"
done < infile
Output:
is a bird
is an animal
is a game.