bash 如何使用正则表达式分隔符获取第 n 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13804849/
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
How to get nth column with regexp delimiter
提问by Ondra
Basically I get line from ls -lacommand:
基本上我从ls -la命令中得到一行:
-rw-r--r-- 13 ondrejodchazel staff 442 Dec 10 16:23 some_file
and want to get size of file (442). I have tried cutand sedcommands, but was unsuccesfull. Using just basic UNIX tools (cut, sed, awk...), how can i get specific column from stdin, where delimiter is / +/regexp?
并想获得文件的大小(442)。我曾尝试cut和sed命令,但unsuccesfull。仅使用基本的 UNIX 工具(cut、sed、awk...),如何从 stdin 获取特定列,其中分隔符是/ +/regexp?
回答by Thor
If you want to do it with cutyou need to squeeze the space first (tr -s ' ') because cutdoesn't support +. This should work:
如果你想这样做,cut你需要先挤压空间 ( tr -s ' ') 因为cut不支持+. 这应该有效:
ls -la | tr -s ' ' | cut -d' ' -f 5
It's a bit more work when doing it with sed(GNU sed):
使用sed(GNU sed)时需要做更多的工作:
ls -la | sed -r 's/([^ ]+ +){4}([^ ]+).*//'
Slightly more finger punching if you use the grep alternative (GNU grep):
如果您使用 grep 替代方案(GNU grep),则手指打孔会稍微多一些:
ls -la | grep -Eo '[^ ]+( +[^ ]+){4}' | grep -Eo '[^ ]+$'
回答by tripleee
Parsing lsoutput is harder than you think. Use a dedicated tool such as statinstead.
解析ls输出比你想象的要难。使用专用工具,例如stat代替。
size=$(stat -c '%s' some_file)
One way ls -la some_file | awk '{print $5}'could break is if numbers use space as a thousands separator (this is common in some European locales).
一种ls -la some_file | awk '{print $5}'可能的方法是数字使用空格作为千位分隔符(这在某些欧洲语言环境中很常见)。
See also Why You Shouldn't Parse the Output of ls(1).
另请参阅为什么不应解析ls(1)的输出。
回答by anubhava
Pipe your output with:
管道输出:
awk '{print }'
Or even better us to use stat command like this (On Mac):
或者甚至更好地我们使用这样的 stat 命令(在 Mac 上):
stat -f "%z" yourFile
Or (on Linux)
或者(在 Linux 上)
stat -c "%s" yourFile
that will output size of file in bytes.
这将以字节为单位输出文件的大小。

