How to remove the last CR char with `cut`

I would like to get part of the string using cut . Here is an example:

 $ echo "foobar" | cut -c1-3 | hexdump -C 00000000 66 6f 6f 0a |foo.| 00000004 

Note the char added at the end of \n .

In this case, it makes no sense to use cut to remove the last char as follows:

 echo "foobar" | cut -c1-3 | rev | cut -c 1- | rev 

I still get this extra and unwanted char, and I would like to avoid using an extra command, for example:

 shasum file | cut -c1-16 | perl -pe chomp 
+1
source share
1 answer

\n added by echo . Use printf instead:

 $ echo "foobar" | od -c 0000000 foobar \n 0000007 $ printf "foobar" | od -c 0000000 foobar 0000006 

It's funny that cut itself also adds a new line:

 $ printf "foobar" | cut -b1-3 | od -c 0000000 foo \n 0000004 

So the solution seems to use printf to output:

 $ printf "%s" $(cut -b1-3 <<< "foobar") | od -c 0000000 foo 0000003 
+4
source

Source: https://habr.com/ru/post/1247190/


All Articles