在命令中用“:”替换空格

时间:2019-03-13 06:58:49

标签: linux text-processing

当我编写一条命令给我这样的输出时。

[root@localhost:~] esxcli storage filesystem list 
Mount Point                                        Volume Name  UUID                                 Mounted  Type             Size          Free
-------------------------------------------------  -----------  -----------------------------------  -------  ------  -------------  ------------
/vmfs/volumes/5bc0c1e3-2ff435d4-533e-3cd92b0a6096  datastore1   5bc0c1e3-2ff435d4-533e-3cd92b0a6096     true  VMFS-6  1992327954432  589864894464
/vmfs/volumes/50696371-d628af73-35da-9c895c95504a               50696371-d628af73-35da-9c895c95504a     true  vfat        261853184      69218304
/vmfs/volumes/8a820f75-48ed9387-b688-b249a3a1691f               8a820f75-48ed9387-b688-b249a3a1691f     true  vfat        261853184     261844992
/vmfs/volumes/5bc0c1e4-863d9324-d91f-3cd92b0a6096               5bc0c1e4-863d9324-d91f-3cd92b0a6096     true  vfat       4293591040    4260233216
/vmfs/volumes/5bc0c1dd-09ac4bba-56c7-3cd92b0a6096               5bc0c1dd-09ac4bba-56c7-3cd92b0a6096     true  vfat        299712512      80486400

现在我要所有以“ /”开头并以“:”代替空格的行

所以我尝试使用命令esxcli storage filesystem list | sed -e 's/\s\+/:/g' |awk '$1 ~ /^\/[a-z]+/ {print $1$2$3$4$5}esxcli storage filesystem list |awk '$1 ~ /^\/[a-z]+/ {i=":";print $1i$2i$3i$4i$5i}'

这给了我这样的输出:

[root@localhost:~] esxcli storage filesystem list | sed -e 's/\s\+/:/g' |awk '$1 ~ /^\/[a-z]+/ {print $1$2$3$4$5}'
/vmfs/volumes/5bc0c1e3-2ff435d4-533e-3cd92b0a6096:datastore1:5bc0c1e3-2ff435d4-533e-3cd92b0a6096:true:VMFS-6:1992327954432:590073561088
/vmfs/volumes/50696371-d628af73-35da-9c895c95504a:50696371-d628af73-35da-9c895c95504a:true:vfat:261853184:69218304
/vmfs/volumes/8a820f75-48ed9387-b688-b249a3a1691f:8a820f75-48ed9387-b688-b249a3a1691f:true:vfat:261853184:261844992
/vmfs/volumes/5bc0c1e4-863d9324-d91f-3cd92b0a6096:5bc0c1e4-863d9324-d91f-3cd92b0a6096:true:vfat:4293591040:4260954112
/vmfs/volumes/5bc0c1dd-09ac4bba-56c7-3cd92b0a6096:5bc0c1dd-09ac4bba-56c7-3cd92b0a6096:true:vfat:299712512:80486400

但是问题是,如果找不到卷名,那么它应该是“ ::”之类的东西。

我想要输出:

/vmfs/volumes/5bc0c1e3-2ff435d4-533e-3cd92b0a6096:datastore1:5bc0c1e3-2ff435d4-533e-3cd92b0a6096:true:VMFS-6:1992327954432:590073561088
/vmfs/volumes/50696371-d628af73-35da-9c895c95504a::50696371-d628af73-35da-9c895c95504a:true:vfat:261853184:69218304
/vmfs/volumes/8a820f75-48ed9387-b688-b249a3a1691f::8a820f75-48ed9387-b688-b249a3a1691f:true:vfat:261853184:261844992
/vmfs/volumes/5bc0c1e4-863d9324-d91f-3cd92b0a6096::5bc0c1e4-863d9324-d91f-3cd92b0a6096:true:vfat:4293591040:4260954112
/vmfs/volumes/5bc0c1dd-09ac4bba-56c7-3cd92b0a6096::5bc0c1dd-09ac4bba-56c7-3cd92b0a6096:true:vfat:299712512:80486400

0 个答案:

没有答案