Using sed
By default, sed
reads newline-separated input. This command will remove everything before the last \r
on a line:
sed 's/.*\r//'
For example:
$ echo $'line1\n\hi\rhello\rworld' | sed 's/.*\r//'
line1
world
To use this with tee
, we can use process substitution:
echo $'line1\n\hi\rhello\rworld' | tee >(sed 's/.*\r//' >Outfile)
Under bash, the construct >(...)
is called process substitution. It runs the commands in the parens and puts them in a file-like object. tee
write to the file-like object and the commands process the input.
Using awk
Similarly, this removes everything before the last \r
on a newline-separated line:
awk -F'\r' '{print $NF}'
The option -F'\r'
sets the field separator to a carriage return. Consequently, we only want to print the last field, $NF
, on each newline-separated line. Thus, print $NF
.
For example:
$ echo $'line1\n\hi\rhello\rworld' | awk -F'\r' '{print $NF}'
line1
world
To use this with tee
:
echo $'line1\n\hi\rhello\rworld' | tee >(awk -F'\r' '{print $NF}' >Outfile)