如何用`cut`删除最后一个CR字符 [英] How to remove the last CR char with `cut`
本文介绍了如何用`cut`删除最后一个CR字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 cut
来获取字符串的一部分。下面是一个虚拟示例:
I would like to get a portion of a string using cut
. Here a dummy example:
$ echo "foobar" | cut -c1-3 | hexdump -C
00000000 66 6f 6f 0a |foo.|
00000004
注意 \n
char添加在末尾。
Notice the \n
char added at the end.
在这种情况下,没有必要使用 cut
删除最后一个字符,如下所示:
In that case there is no point to use cut
to remove the last char as follow:
echo "foobar" | cut -c1-3 | rev | cut -c 1- | rev
我仍然会得到这个多余的不需要的字符,我想避免使用诸如如:
I will still get this extra and unwanted char and I would like to avoid using an extra command such as:
shasum file | cut -c1-16 | perl -pe chomp
推荐答案
$ n
由 echo
添加。而是使用 printf
:
$ echo "foobar" | od -c
0000000 f o o b a r \n
0000007
$ printf "foobar" | od -c
0000000 f o o b a r
0000006
cut
本身也添加了新行:
$ printf "foobar" | cut -b1-3 | od -c
0000000 f o o \n
0000004
所以解决方案似乎使用 printf
到其输出:
So the solution seems using printf
to its output:
$ printf "%s" $(cut -b1-3 <<< "foobar") | od -c
0000000 f o o
0000003
这篇关于如何用`cut`删除最后一个CR字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文