简体   繁体   English

排除awk无法正常工作的文件夹

[英]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 : 根据您的要求,您可以将此printfxargs

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM