San*_*086 5 text-processing cut
可能的重复:
在单个命令中组合 2 个不同的剪切输出?
我有一个文件 - temp.txt - 其中包含
ABCDF
PQRST
LMNOP
Run Code Online (Sandbox Code Playgroud)
我试过这个cut
命令
$ cut -c 2-5 temp.txt
BCDF
QRST
MNOP
Run Code Online (Sandbox Code Playgroud)
输出是对的。
$ cut -c 1,2,3,4 temp.txt
ABCD
PQRS
LMNO
Run Code Online (Sandbox Code Playgroud)
输出是对的。
但是当我改变顺序时,意思是
$ cut -c 2-4,1 temp.txt
ABCD
PQRS
LMNO
Run Code Online (Sandbox Code Playgroud)
以上输出显示。
但逻辑输出是
BCDA
QRSP
MNOL
Run Code Online (Sandbox Code Playgroud)
所以我的问题是这是怎么发生的,背后的任何原因。
解释在 中man cut
,但很容易将其作为您的首选期望来阅读。然而它确实说:
Selected input is written in the same order that it is read
Run Code Online (Sandbox Code Playgroud)
关于 input 的含义可能不明确,但通常 Options 只是那个, options ,而正在处理的文件肯定是input。
的list
,如。-c 2,3,4,1
是非常喜欢的正则表达式列表:[2341]
; 这意味着列表中的任何数字,即使它们被加倍......例如。-c 2,2,2,2,2,2
与-c 2
..相同,对于正则表达式,[2222222]
与[2]
...相同,因此您的选项顺序无关紧要。
小智 5
解决办法是:
sed 's:^\(.\)\(.*\):\2\1:' file.txt
Run Code Online (Sandbox Code Playgroud)
(昨天有人发布了对类似问题的回答)
Cut 似乎不排序输出,只选择列/字段的范围并以与它们相同的顺序输出。