bash 我可以更改 Linux cut 命令中输出字段的顺序吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14112120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I change the order of the output fields from the Linux cut command?
提问by Alexis Cimpu
I am using cut command in command line and seems I can't get the output I like. Do you have any idea why I am getting this? Is it something that I do wrong?
我在命令行中使用 cut 命令,似乎无法获得我喜欢的输出。你知道我为什么会得到这个吗?我做错了什么吗?
This is the normal output and I would like to output in different order:
这是正常输出,我想以不同的顺序输出:
[root@upbvm500 root]# ls -al IDS_DIR/a | tr -s " "
-rw-r--r-- 1 root root 0 Jan 1 17:18 IDS_DIR/a
[root@upbvm500 root]#
[root@upbvm500 root]# ls -al IDS_DIR/a | tr -s " " | cut -d" " -f5,6,7,8,3,4,1
-rw-r--r-- root root 0 Jan 1 17:18
But as you can see, this is not working like expected. Any idea why they are switching places?
但是正如您所看到的,这并不像预期的那样工作。知道他们为什么要换地方吗?
回答by Chris Seymour
From man cut:
来自man cut:
Selected input is written in the same order that it is read, and is written exactly once.
所选输入的写入顺序与读取顺序相同,并且只写入一次。
Use awk '{print $5,$6,$7,$8,$3,$4,$1}'instead of cut.
使用awk '{print $5,$6,$7,$8,$3,$4,$1}'代替cut。
回答by tripleee
cutdoes not reorder its output. It simply collects a list of which columns to print, then prints them out as they arrive.
cut不重新排序其输出。它只是收集要打印的列的列表,然后在它们到达时将它们打印出来。
Use a different tool such as Awk to reorder output columns.
使用不同的工具(例如 Awk)对输出列重新排序。
However, in this patricular case, try with stator findinstead of ls. It is generally not recommended to try to parse the output from ls. See http://mywiki.wooledge.org/ParsingLs
但是,在这种特殊情况下,请尝试使用stat或find代替ls。通常不建议尝试从ls. 见http://mywiki.wooledge.org/ParsingLs
回答by glenn Hymanman
As others have mentioned, don't parse ls. If you want file information, use stat
正如其他人所提到的,不要解析 ls。如果您需要文件信息,请使用stat
stat -c "%s %y %U %G %A %n" filename
You may need to do some extra work to get the timestamp formatted as you want.
您可能需要做一些额外的工作来根据需要格式化时间戳。
$ ls -l data
-rw-r--r-- 1 glennj glennj 13 2013-01-01 11:19 data
$ LC_TIME=POSIX ls -l data
-rw-r--r-- 1 glennj glennj 13 Jan 1 11:19 data
$ stat -c "%s %y %U %G %A %n" data
13 2013-01-01 11:19:53.670015242 -0500 glennj glennj -rw-r--r-- data
$ stat -c "%s %Y %U %G %A %n" data | awk '{ = strftime("%b %e %H:%M", )} 1'
13 Jan 1 11:19 glennj glennj -rw-r--r-- data

