[英]Formatting ls output without tab using awk?
My question may seem fairly simple, I'm a beginner at bash scripting still, but I'm trying to format my ls output without using a tab when I print with awk. 我的问题可能看起来相当简单,我仍然是bash脚本的初学者,但我正在尝试使用awk打印时不使用选项卡来格式化我的ls输出。
Here is my current command that I'm running: 这是我正在运行的当前命令:
ls -al | sed 1d | awk '{print $1" ",$6" ", $7"\t", $9}'
and it prints out the following 它打印出以下内容
drwx------ Feb 13 .
drwxr-xr-x Feb 5 ..
-rw-r--r-- Jan 21 .alias
-rw-r--r-- Feb 13 .bash_history
-rw-r--r-- Jan 29 .bash_profile
-rw-r--r-- Jan 21 .bash_profile~
-rw-r--r-- Feb 13 .bashrc
-rw-r--r-- Jan 21 .bashrc~
drwxr-xr-x Jan 3 csc135
drwxr-xr-x Aug 13 csc136
drwxr-xr-x Feb 9 csc235
drwxr-xr-x Oct 19 csc237
drwxr-xr-x Feb 18 csc310
drwxr-xr-x Feb 6 csc325
I have also attempted using a space: 我也尝试过使用空格:
ls -al | sed 1d | awk '{print $1" ",$6" ", $7" ", $9}'
which prints the following (quite bothersome): 打印以下(非常麻烦):
drwx------ Feb 13 .
drwxr-xr-x Feb 5 ..
-rw-r--r-- Jan 21 .alias
-rw-r--r-- Feb 13 .bash_history
-rw-r--r-- Jan 29 .bash_profile
-rw-r--r-- Jan 21 .bash_profile~
-rw-r--r-- Feb 13 .bashrc
-rw-r--r-- Jan 21 .bashrc~
drwxr-xr-x Jan 3 csc135
drwxr-xr-x Aug 13 csc136
drwxr-xr-x Feb 9 csc235
drwxr-xr-x Oct 19 csc237
drwxr-xr-x Feb 18 csc310
drwxr-xr-x Feb 6 csc325
Is there any way to make it nicer looking like this? 有没有办法让它看起来更漂亮?
drwx------ Feb 13 .
drwxr-xr-x Feb 5 ..
-rw-r--r-- Jan 21 .alias
-rw-r--r-- Feb 13 .bash_history
-rw-r--r-- Jan 29 .bash_profile
-rw-r--r-- Jan 21 .bash_profile~
-rw-r--r-- Feb 13 .bashrc
-rw-r--r-- Jan 21 .bashrc~
drwxr-xr-x Jan 3 csc135
drwxr-xr-x Aug 13 csc136
drwxr-xr-x Feb 9 csc235
drwxr-xr-x Oct 19 csc237
drwxr-xr-x Feb 18 csc310
drwxr-xr-x Feb 6 csc325
One automatic option that comes to mind is column
: 想到的一个自动选项是
column
:
$ ls -al | sed 1d | awk '{print $1,$6,$7,$9}' | column -t
drwxr-xr-x+ Feb 3 .
drwxrwxrwt+ Oct 9 ..
-rw------- Feb 12 .bash_history
-rwxr-xr-x Oct 9 .bash_profile
-rwxr-xr-x Feb 3 .bashrc
drwx------+ Feb 2 .cache
-rwxr-xr-x Oct 9 .inputrc
-rwxr-xr-x Feb 14 .mkshrc
-rwxr-xr-x Oct 9 .profile
drwxrwx---+ Dec 13 .ssh
Note the -t
flag used for column
: 请注意用于
column
的-t
标志:
-t, --table Determine the number of columns the input contains and create a table.
-t, - table确定输入包含的列数并创建表。 Columns are delimited with whitespace, by default, or with the characters supplied using the --output-separator option.
默认情况下,列用空格分隔,或使用--output-separator选项提供的字符分隔。 Table output is useful for pretty-printing.
表输出对于漂亮打印很有用。
Another way would be to used fixed lengths: 另一种方法是使用固定长度:
$ ls -al | sed 1d | awk '{printf "%-12s %-3s %-2s %s\n", $1,$6,$7,$9}'
drwxr-xr-x+ Feb 3 .
drwxrwxrwt+ Oct 9 ..
-rw------- Feb 12 .bash_history
-rwxr-xr-x Oct 9 .bash_profile
-rwxr-xr-x Feb 3 .bashrc
drwx------+ Feb 2 .cache
-rwxr-xr-x Oct 9 .inputrc
-rwxr-xr-x Feb 14 .mkshrc
-rwxr-xr-x Oct 9 .profile
drwxrwx---+ Dec 13 .ssh
There is another example of this in the GNU Awk User's guide . GNU Awk用户指南中还有另一个例子。
ls -al|awk 'NR>1{print $1,$6,$7,$9}'|column -t
you don't need to use sed to delete the first line. 你不需要使用sed来删除第一行。 Awk can do this if NR>1 print
如果NR> 1打印,Awk可以执行此操作
You can avoid all unnecessary pipes by using features of awk
. 您可以使用
awk
功能来避免所有不必要的管道。
ls -al | awk -v OFS="\t" 'NR>1 { print $1, $6, $7, $9 }'
NR>1
to get rid of the unnecessary sed
to delete the first row. NR>1
摆脱不必要的sed
以删除第一行。 OFS
variable to set the output field separator to tab there by removing the need for column
. OFS
变量通过删除column
的需要将输出字段分隔符设置为tab。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.