我可以从Linux cut命令更改输出字段的顺序吗?

Ale*_*mpu 8 linux bash shell awk cut

我在命令行中使用cut命令,似乎我无法得到我喜欢的输出.你知道我为什么这么做吗?这是我做错了吗?

这是正常输出,我想以不同的顺序输出:

[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
Run Code Online (Sandbox Code Playgroud)

但正如你所看到的,这并不像预期的那样有效.知道他们为什么要转换位置吗?

Chr*_*our 22

来自man cut:

选定的输入按照与读取相同的顺序写入,并且只写入一次.

awk '{print $5,$6,$7,$8,$3,$4,$1}'而不是cut.


tri*_*eee 6

cut不重新排序其输出.它只是收集要打印的列的列表,然后在它们到达时打印出来.

使用其他工具(如Awk)重新排序输出列.

但是,在这种情况下,请尝试使用statfind代替ls.通常不建议尝试解析输出ls.请参阅http://mywiki.wooledge.org/ParsingLs


gle*_*man 6

正如其他人所提到的,不要解析ls.如果您需要文件信息,请使用stat

stat -c "%s %y %U %G %A %n" filename
Run Code Online (Sandbox Code Playgroud)

您可能需要做一些额外的工作来根据需要格式化时间戳.

$ 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 '{$2 = strftime("%b %e %H:%M", $2)} 1'
13 Jan  1 11:19 glennj glennj -rw-r--r-- data
Run Code Online (Sandbox Code Playgroud)