[英]Adding a column with awk
I want to add a new column at the beggining of every row. 我想在每一行的开头添加一个新列。 Command used:
使用的命令:
tree -afispugD --inodes
树-afispugD --inodes
I want to put a new column which will be the name of the file. 我想放置一个新列,它将作为文件的名称。
Example: 例:
119801433 -rwxr--r-- u1915811 alum 1252 Apr 1 21:06 ./file
119802284 -rw-r--r-- u1915811 alum 1255 Mar 20 10:25 ./text.txt
119865862 drwxr-xr-x u1915811 alum 4096 Feb 27 10:20 ./S3/folder2
To this: 对此:
file 119801433 -rwxr--r-- u1915811 alum 1252 Apr 1 21:06 ./file
text.txt 119802284 -rw-r--r-- u1915811 alum 1255 Mar 20 10:25 ./text.txt
folder2 119865862 drwxr-xr-x u1915811 alum 4096 Feb 27 10:20 ./S3/folder2
PS: I have to do it because tree
command doesn't show names :( PS:我必须这样做,因为
tree
命令不显示名称:(
All you need is: 所有你需要的是:
$ awk -F'/' '{print $NF,$0}' file
file 119801433 -rwxr--r-- u1915811 alum 1252 Apr 1 21:06 ./file
text.txt 119802284 -rw-r--r-- u1915811 alum 1255 Mar 20 10:25 ./text.txt
folder2 119865862 drwxr-xr-x u1915811 alum 4096 Feb 27 10:20 ./S3/folder2
or if you want to use some specific spacing in the output use printf instead of print: 或者,如果您想在输出中使用一些特定的间距,请使用printf而不是print:
$ awk -F'/' '{printf "%-10s%s\n",$NF,$0}' file
file 119801433 -rwxr--r-- u1915811 alum 1252 Apr 1 21:06 ./file
text.txt 119802284 -rw-r--r-- u1915811 alum 1255 Mar 20 10:25 ./text.txt
folder2 119865862 drwxr-xr-x u1915811 alum 4096 Feb 27 10:20 ./S3/folder2
or, since this is a simple substitution on a single line, you could use sed instead of awk: 或者,由于这是在一行上的简单替换,因此您可以使用sed而不是awk:
$ sed 's/\(.*\/\(.*\)\)/\2 \1/' file
file 119801433 -rwxr--r-- u1915811 alum 1252 Apr 1 21:06 ./file
text.txt 119802284 -rw-r--r-- u1915811 alum 1255 Mar 20 10:25 ./text.txt
folder2 119865862 drwxr-xr-x u1915811 alum 4096 Feb 27 10:20 ./S3/folder2
如果文件名称中包含空格或符号链接,则此文件也可以使用
tree -afispugD --inodes | awk '{FS="./"; ORS=""; printf("%-60s%s\n",$NF,$0)}'
在文件名中没有空格之前,这应该起作用:
tree -afispugD --inodes | awk '{printf("-30s%s\n",$NF,$0}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.