Answer How to remove the last CR char with cut I found out that some programs add a new trailing line to the end of the line, while others do not:
Say we have a foobar string and print it with printf so that we don't get an extra new line:
$ printf "foobar" | od -c 0000000 foobar 0000006
Or using echo -n :
$ echo -n "foobar" | od -c 0000000 foobar 0000006
(The echo default behavior is to return output, followed by a newline, so echo "foobar" returns foobar \n ).
Neither sed nor cat add an extra character:
$ printf "foobar" | sed 's/./&/g' | od -c 0000000 foobar 0000006 $ printf "foobar" | cat - | od -c 0000000 foobar 0000006
While both awk and cut do. Also xargs and paste add this new new line:
$ printf "foobar" | cut -b1- | od -c 0000000 foobar \n 0000007 $ printf "foobar" | awk '1' | od -c 0000000 foobar \n 0000007 $ printf "foobar" | xargs | od -c 0000000 foobar \n 0000007 $ printf "foobar" | paste | od -c 0000000 foobar \n 0000007
So I was wondering: why is this a different behavior? Does POSIX have anything to offer about this?
Note. I do all this in my Bash 4.3.11, and the rest:
- GNU Awk 4.0.1
- sed (GNU sed) 4.2.2
- cat (GNU coreutils) 8.21
- cut (GNU coreutils) 8.21
- xargs (GNU findutils) 4.4.2
- paste (GNU coreutils) 8.21
source share