[英]Exclude folder with awk not working properly
I've to make a folder contents list using ls -alis
and using awk
to filter the results. 我必须使用
ls -alis
并使用awk
过滤结果来制作文件夹内容列表。
I'm having some problems with $11 != "." && $11 != ".."
$11 != "." && $11 != ".."
我遇到了一些问题$11 != "." && $11 != ".."
$11 != "." && $11 != ".."
. $11 != "." && $11 != ".."
。 I don't want to display the folder .
我不想显示该文件夹
.
and the folder ..
but they are still here. 和文件夹
..
但它们仍然在这里。
ls --color -alis /home/user/ |
tr -s ' ' | cut -d " " -f11 |
awk '$1 !~"^/\./$" { print $1 }'
I've tried $11 !~ "^/./$"
as well. 我也尝试过
$11 !~ "^/./$"
。
Parsing output of ls
is not recommended as filenames can have white-spaces newlines etc and will break your piped commands. 不建议解析
ls
输出,因为文件名可以包含空格,换行符等,并且会破坏管道命令。
For your requirement you can just use this printf
with xargs
: 根据您的要求,您可以将此
printf
与xargs
:
printf "%s\0" * | xargs -0 ls --color -Alis
Don't double quote the regex, and the anchor points ( ^
and $
) go inside the regex: 不要用双引号括住正则表达式,并且锚点(
^
和$
)放在正则表达式内:
awk '$11 !~ /^\.\.?$/ { print }'
That doesn't match a line where field 11 contains one or two dots (only). 这与字段11包含一个或两个点(仅)的行不匹配。
This deals with the problems with the awk
script; 这解决了
awk
脚本的问题; it is, however, debatable whether you should be parsing the output of ls
as it is can be ambiguous. 但是,是否应该解析
ls
的输出是不确定的,因为它可能是模棱两可的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.