bash awk '{print $9}' 最后一个 ls -l 列,包括文件名中的任何空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1447809/
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
awk '{print $9}' the last ls -l column including any spaces in the file name
提问by Mint
How would I get awk to output the whole file name in ls -l
if some of the files have spaces in them. Usually, I can run this command:
ls -l
如果某些文件中有空格,我将如何让 awk 输出整个文件名。通常,我可以运行以下命令:
ls -l | awk '{print }'
That doesn't work if the files have spaces in them. Is it possible to print $9, $10, $11 etc as well somehow?
如果文件中有空格,那将不起作用。是否可以以某种方式打印 9 美元、10 美元、11 美元等?
回答by Charles Duffy
A better solution: Don't attempt to parse ls output in the first place.
更好的解决方案:首先不要尝试解析 ls 输出。
The official wiki of the irc.freenode.org #bash channel has an explanation of why this is a Bad Idea, and what alternate approaches you can take instead: http://mywiki.wooledge.org/ParsingLs
irc.freenode.org #bash 频道的官方 wiki 解释了为什么这是一个坏主意,以及您可以采取哪些替代方法: http://mywiki.wooledge.org/ParsingLs
Use of find, statand similar tools will provide the functionality you're looking for without the pitfalls (not all of which are obvious -- some occur only when moving to platforms with different ls implementations).
使用 find、stat和类似工具将提供您正在寻找的功能而没有陷阱(并非所有这些都是显而易见的——有些只有在移动到具有不同 ls 实现的平台时才会出现)。
For your specific example, I'm guessing that you're trying to find only files (and not directories) in your current directory; your current implementation using ls -l
is buggy, as it excludes files which have +t or setuid permissions. The Right Way to implement this would be the following:
对于您的具体示例,我猜您正在尝试在当前目录中仅查找文件(而不是目录);您当前使用的实现ls -l
有问题,因为它排除了具有 +t 或 setuid 权限的文件。实现这一点的正确方法如下:
find . -maxdepth 1 -type f -printf '%f\n'
回答by Mint
Just for completion. It can also be done with sed:
只为完成。也可以用 sed 来完成:
# just an exercise in regex matching ...
ls -l | sed -E -e '1d; s/^([^ ]+ +){8}//'
回答by Mark Rushakoff
There's probably a better approach that involves combining fields somehow, but:
可能有更好的方法涉及以某种方式组合字段,但是:
$ echo 1 2 3 4 5 6 7 8 9 10 11 12 13 14... | awk '{for (i = 9 ; i <= NF ; i++) printf "%s ", $i}' 9 10 11 12 13 14...
Using printf "%s " $i
will print the i-th field with a space after it, instead of a newline. The for loop just says to go from field 9 to the last field.
Usingprintf "%s " $i
将打印第 i 个字段,后面有一个空格,而不是换行符。for 循环只是说从字段 9 到最后一个字段。
回答by Hai Vu
If you still insist on the ls -l instead of find or other tools, here is my solution. It is not pretty and destructive:
如果您仍然坚持使用 ls -l 而不是 find 或其他工具,这是我的解决方案。它既不漂亮也不具有破坏性:
- Destroy $1 .. $8 by setting them to "" via a for loop
- That leaves a bunch of spaces preceding $9, remove them using the sub() command
- Print out the remaining
- 通过 for 循环将它们设置为 "" 来销毁 $1 .. $8
- 这在 $9 之前留下一堆空格,使用 sub() 命令删除它们
- 打印出剩余的
ls -l | awk '{for (i = 1; i < 9; i++) $i = ""; sub(/^ */, ""); print}'
回答by Amro
echo 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 |
awk 'BEGIN {OFS=ORS=""} ; {for (i=9;i<NF;i++) print $i " "; print $NF "\n"}'
回答by Pithikos
A solution is to encode & decode the space with a word or character by using sed:
一种解决方案是使用 sed 对带有单词或字符的空格进行编码和解码:
ls -l | sed s/\ /{space}/ | awk '{print }' | sed s/{space}/\ /
This will replace all spaces in a line with {space}
before passing it to awk. After the line has passed to awk, we replace {space}
back with space.
这将{space}
在将其传递给 awk 之前替换一行中的所有空格。在该行传递给 awk 之后,我们{space}
用空格替换back。
find
as stated by others is a much better solution. But if you really have to use awk, you can try this.
find
正如其他人所说,这是一个更好的解决方案。但是如果你真的必须使用awk,你可以试试这个。
回答by user5453829
ls -l | awk -v x=9 '{print $x}'
I use this for getting filenames of a directory without incident. I noted the find solution which is fine and dandy if you are unsure of the file types, if you know what you are looking at the ls -l works just fine, it also alphabetically orders by default.
我使用它来获取目录的文件名而不会发生任何事故。如果您不确定文件类型,我注意到 find 解决方案很好,很花哨,如果您知道自己在看什么 ls -l 工作得很好,默认情况下它也按字母顺序排序。