Pau*_*cks 7 text-processing cut
我喜欢cut在 Linux 中使用带有-c标志的命令。但是,我有兴趣找到一个与cut. 基本上,给定输入:
drwxrwxrwx 2 root root 4096 4096 4 20:15 bin
drwxrwxrwx 2 root root 4096 4096 4 20:15 Desktop
Run Code Online (Sandbox Code Playgroud)
我想看到除了“4096 4 20:15”之外的所有内容。这是输出:
drwxrwxrwx 2 root root bin
drwxrwxrwx 2 root root Desktop
Run Code Online (Sandbox Code Playgroud)
如果有意义的话,我希望能够从字面上切出字符 x 和 y 之间。
有任何想法吗?我无法想象这是一个很难编写的脚本,但如果已经存在一个命令,我很乐意使用它。
ter*_*don 10
正如其他人指出的那样,您不应该解析ls. 假设您ls仅用作示例并且将解析其他内容,则有几种方法可以执行您想要的操作:
cut与 -d和-f
cut -d ' ' -f 1,2,3,4,9
Run Code Online (Sandbox Code Playgroud)
来自man cut:
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
-f, --fields=LIST
select only these fields; also print any line
that contains no delimiter character, unless
the -s option is specified
Run Code Online (Sandbox Code Playgroud)
专门ls为此可能会失败,因为ls将更改连续字段之间的空白量以使它们更好地对齐。cut对待foo<space>bar和foo<space><space>bar不同。
awk 及其变体将每个输入行拆分为空白区域,这样您就可以告诉它只打印您想要的字段:
awk '{print $1,$2,$3,$4,$9}'
Run Code Online (Sandbox Code Playgroud)珀尔
perl -lane 'print "@F[0 .. 3,8]"'
Run Code Online (Sandbox Code Playgroud)