bash 切割列包括尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16374616/
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
Cutting the column including size
提问by user2344333
I want to cut the column which include the size of files . I use ls -lto view info about files in current localization . I save info about file in txt file ls -l > info.txt, and now I want to cut the column including size . I do cat info.txt | cut -d'' -f6but i don't work . What I do wrong ?
我想剪切包含文件大小的列。我ls -l用来查看有关当前本地化文件的信息。我将有关文件的信息保存在 txt 文件中ls -l > info.txt,现在我想剪切包含大小的列。我有,cat info.txt | cut -d'' -f6但我不工作。我做错了什么?
This include info.txt :
这包括 info.txt :
-rwx------ 1 s10891 Domain Users 2188 May 4 14:51 info.txt
-rwx------ 1 s10891 Domain Users 1640 Mar 10 07:43 code.txt
-rwx------ 1 s10891 Domain Users 68 Mar 4 11:59 sorted.txt
drwx------ 1 s10891 Domain Users 0 Jan 11 09:48 PPJ
drwx------ 1 s10891 Domain Users 0 Sep 7 2012 public_html
drwx------ 1 s10891 Domain Users 0 Apr 15 11:16 RBD
drwx------ 1 s10891 Domain Users 0 Jan 7 09:45 RDB
drwx------ 1 s10891 Domain Users 0 Apr 15 12:00 SOP
drwx------ 1 s10891 Domain Users 0 Apr 8 12:53 sop_ex3
-rwx------ 1 s10891 Domain Users 122 Feb 25 11:48 sop_info.txt
drwx------ 1 s10891 Domain Users 0 Jan 14 09:41 WindowsFormsApplicati
drwx------ 1 s10891 Domain Users 0 Jan 14 09:41 WindowsFormsApplicati
drwx------ 1 s10891 Domain Users 0 Jan 14 09:41 WindowsFormsApplicati
and I want to get this :
我想得到这个:
2188
1640
68
0
0
0
0
0
0
122
0
0
0
回答by shellter
most cuts count literal characters, so for -f 6to work, your data has to be exactly in the same columns. Not sure if your 3rd to last line is a typo or an exact reproduction of your output, but it illustrates the problem with cut perfectly.
大多数cuts 计数文字字符,因此为了-f 6工作,您的数据必须完全在相同的列中。不确定您的倒数第三行是拼写错误还是输出的精确复制,但它完美地说明了 cut 的问题。
For this case, most people will use an awksolution:
对于这种情况,大多数人会使用一个awk解决方案:
ls -l | awk '{print }'
Will produce the output you have listed
将产生您列出的输出
The beauty of awk is that field 6 is determined by the value of awk FS variable (field separator), which defaults to "multiple white space values" (space or tab) (this is not an exact description, but is close enough for your needs).
awk 的美妙之处在于字段 6 是由 awk FS 变量(字段分隔符)的值决定的,它默认为“多个空白值”(空格或制表符)(这不是一个准确的描述,但对于您的需要)。
回答by glenn Hymanman
You don't need to parse ls: stat -c %s *will give you a column of sizes. If you want the filename too: stat -c "%s %n" *
你不需要解析ls:stat -c %s *会给你一列大小。如果你也想要文件名:stat -c "%s %n" *
回答by Carlos Contreras
With sed:
使用 sed:
ls -l | sed -E 's/ +/ /g'
" +" means any multiple space
“+”表示任意多个空格

