本文介绍了外壳程序必须解析ls -Al输出并获取最后一个字段(文件或目录名称)任何解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须解析 ls -Al输出并获取文件或目录名称

I must parse ls -Al output and get file or directory name

ls -Al 输出:

drwxr-xr-x  12 s162103  studs         12 march 28 2012 personal domain
drwxr-xr-x   2 s162103  studs          3 march 28 22:32 public_html
drwxr-xr-x   7 s162103  studs          8 march 28 13:59 WebApplication1

我应该只使用 ls -Al |< something>

例如:

ls -Al | awk '{print $8}'

但这不起作用,因为如果目录名称中有空格,则 $ 8 不是名称,它是名称的一部分.也许有一些实用程序可以剪切姓氏或之前删除任何内容?我需要找到任何解决方案.请帮忙!

but this doesn't work because $8 is not name if there's spaces in directory name,it is a part of name. maybe there's some utilities that cut last name or delete anything before? I need to find any solution. Please, help!

我知道解析ls -Al是个坏主意,但是我应该使用上面的构造来解析它!没办法使用这样的东西

EDITED: I know what parse ls -Al is bad idea but I should exactly parse it with construction above! No way to use some thing like this

for f in *; do
    somecommand "$f"
done

推荐答案

如何处理 ls -Al | awk'{$ 1 = $ 2 = $ 3 = $ 4 = $ 5 = $ 6 = $ 7 = $ 8 =";打印$ 0}'我知道这是个便宜的把戏,但是既然您不想使用 ls -Al 以外的任何东西,那么我想不出更好的方法了……

How about thisls -Al |awk '{$1=$2=$3=$4=$5=$6=$7=$8="";print $0}' I know it's a cheap trick but since you don't want to use anything other than ls -Al I cant think anything better...

这篇关于外壳程序必须解析ls -Al输出并获取最后一个字段(文件或目录名称)任何解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:15