[英]AWK command does not join two outputs normally
I have a simple task to join output from the lsblk
command with output from the df
command on the field which contains the mount point.我有一个简单的任务是将
lsblk
命令的输出与包含挂载点的字段上的df
命令的输出连接起来。
Example output from df
: df
输出示例:
$ df -P --exclude={tmpfs,devtmpfs,squashfs,overlay} | sed /^Filesystem/d
/dev/mapper/dockerVG-rootLV 51339744 8118908 40583220 17% /
/dev/mapper/ssdVG-ssdLV 515006720 214044372 274731772 44% /dockerssd
/dev/mapper/hddVG-hddLV 1547129060 83285716 1385183760 6% /dockerhdd
/dev/mapper/hddVG-dockerLV 515006720 76061148 412714996 16% /var/lib/docker
Output of lsblk
: lsblk
输出:
$ lsblk -n -b --output KNAME,NAME,SIZE,MOUNTPOINT | egrep -v "fd0|ram|loop|sr0|hdc|cdrom|[SWAP]"
sda sda 53687091200
sda1 └─sda1 53684994048
dm-3 └─dockerVG-rootLV 53682896896 /
sdb sdb 2147483648000
sdb1 └─sdb1 2147482599424
dm-1 ├─hddVG-dockerLV 536866717696 /var/lib/docker
dm-2 └─hddVG-hddLV 1610612736000 /dockerhdd
sdc sdc 536870912000
sdc1 └─sdc1 536869863424
dm-0 └─ssdVG-ssdLV 536866717696 /dockerssd
I am using Awk;我正在使用 awk; here is my current code:
这是我当前的代码:
awk '{ print $1 " " $3 " " $4 " " arr[$4] }
/^\// { arr[$6]=$1 " " $2 " " $3 " " $4 " " $5 }' <<< $(
df -P --exclude={tmpfs,devtmpfs,squashfs,overlay} |
sed -e /^Filesystem/d
lsblk -n -b --output KNAME,NAME,SIZE,MOUNTPOINT |
egrep -v "fd0|ram|loop|sr0|hdc|cdrom|[SWAP]")
Here's the output I currently get:这是我目前得到的输出:
/dev/mapper/dockerVG-rootLV 8118908 40583220
/dev/mapper/ssdVG-ssdLV 214044380 274731764
/dev/mapper/hddVG-hddLV 83285716 1385183760
/dev/mapper/hddVG-dockerLV 76061152 412714992
sda 53687091200
sda1 53684994048
dm-3 53682896896 / /dev/mapper/dockerVG-rootLV 51339744 8118908 40583220 17%
sdb 2147483648000
sdb1 2147482599424
dm-1 536866717696 /var/lib/docker /dev/mapper/hddVG-dockerLV 515006720 76061152 412714992 16%
dm-2 1610612736000 /dockerhdd /dev/mapper/hddVG-hddLV 1547129060 83285716 1385183760 6%
sdc 536870912000
sdc1 536869863424
dm-0 536866717696 /dockerssd /dev/mapper/ssdVG-ssdLV 515006720 214044380 274731764 44%
I don't need to have this part of the output:我不需要有这部分输出:
/dev/mapper/dockerVG-rootLV 8118908 40583220
/dev/mapper/ssdVG-ssdLV 214044380 274731764
/dev/mapper/hddVG-hddLV 83285716 1385183760
/dev/mapper/hddVG-dockerLV 76061152 412714992
For me, it is enough to have:对我来说,拥有:
sda 53687091200
sda1 53684994048
dm-3 53682896896 / /dev/mapper/dockerVG-rootLV 51339744 8118908 40583220 17%
sdb 2147483648000
sdb1 2147482599424
dm-1 536866717696 /var/lib/docker /dev/mapper/hddVG-dockerLV 515006720 76061152 412714992 16%
dm-2 1610612736000 /dockerhdd /dev/mapper/hddVG-hddLV 1547129060 83285716 1385183760 6%
sdc 536870912000
sdc1 536869863424
dm-0 536866717696 /dockerssd /dev/mapper/ssdVG-ssdLV 515006720 214044380 274731764 44%
Could You help me please, how to divide this output?你能帮我吗,如何划分这个输出? Maybe some fix in my Awk command?
也许在我的 Awk 命令中有一些修复?
Your syntax is rather weird.你的语法很奇怪。 Probably refactor along the lines of
可能按照以下方式重构
awk 'NR == FNR {
if ($1 ~ /^\//) arr[$6]=$1 " " $2 " " $3 " " $4 " " $5
next }
/fd0|ram|loop|sr0|hdc|cdrom|\[SWAP\]/ { next }
{ print $1 " " $3 " " $4 " " arr[$4] }' \
<(df -P --exclude={tmpfs,devtmpfs,squashfs,overlay}) \
<(lsblk -n -b --output KNAME,NAME,SIZE,MOUNTPOINT)
Filesystem
doesn't match ^/
so I took that out. Filesystem
与^/
不匹配,所以我把它去掉了。 The regex [SWAP]
was incorrect;正则表达式
[SWAP]
不正确; I assume you meant to match that literally.我假设你的意思是从字面上匹配。 Awk can do everything
egrep
and sed
can do, so I refactored those out. Awk 可以做
egrep
和sed
可以做的一切,所以我重构了它们。
The beef here is the next
inside the first condition, to avoid printing those lines.这里的牛肉是第
next
条件中的next
一个,以避免打印这些行。 The refactoring to read two separate file handles also makes the script somewhat more idiomatic and, hopefully, understandable.重构以读取两个单独的文件句柄也使脚本更加惯用,并且希望可以理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.