bash - Parse/proc/mounts 和 substitude 只有一个字段

标签 bash awk substitution

我正在寻找一种从/proc/mounts 打印信息的方法:

/home     /dev/md9    /dev/mapper/home    home
/var/tmp  /dev/md7    /dev/mapper/vartmp  vartmp

我尝试:

awk '{ print $2 " " $1; gsub("/","",$2); print "/dev/mapper/"$2" "$2 }' /proc/mounts

但结果是两行:

/home /dev/mapper/home
/dev/mapper/home home
/var/tmp /dev/md7
/dev/mapper/vartmp vartmp

有没有人有解决办法?

最佳答案

修复

  • use printf ( to avoid the implicit linefeed )
  • add whitespace to separate the printf from the gsub output

调整命令

awk '{ printf $2 " " $1 " "; gsub("/","",$2); print "/dev/mapper/"$2" "$2 }' /proc/mounts

输入.txt

/dev/mapper/home /home blah blah blah blah

输出

$ awk '{ printf $2 " " $1 " "; gsub("/","",$2); print "/dev/mapper/"$2" "$2 }' input.txt 
/home /dev/mapper/home /dev/mapper/home home

关于bash - Parse/proc/mounts 和 substitude 只有一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769432/

相关文章:

c - 在 ubuntu 终端中同步运行两个 C 脚本

linux - sed:如何替换多行模式中的所有内容?

打印 ${IFS@Q} 时出现 Bash 异常

shell - SED 或 AWK 全部替换为另一个文件中的模式

linux - 如何使用 Bash(sed 或任何其他)命令在第 EOF-n 行插入

string - 如何在命令中正确引用字符串以进行替换?

function - R 中 `substitute` 的令人困惑的行为

linux - Bash:无法使用计算识别具有范围的文件名

linux - 如果特定字符串在 bash 中使用 sed 在模式之间匹配,则在前后添加字符串

regex - 尝试使用 perl 进行替换