我想删除出现在 awk 输出中的引号(双引号),如何实现
# systool -c fc_host -v | awk '/Class Device =/{host=$4}/port_state/{print host,$3}' (This is my awk output sorted)
"host1" "Online"
"host2" "Online"
Run Code Online (Sandbox Code Playgroud)
下面是命令和命令输出..
# systool -c fc_host -v
Class Device = "host1"
Class Device path = "/sys/class/fc_host/host1"
active_fc4s = "0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 "
fabric_name = "0x100000051ee8aecf"
issue_lip = <store method only>
maxframe_size = "2048 bytes"
node_name = "0x20000000c98f62a7"
port_id = "0x652500"
port_name = "0x10000000c98f62a7"
port_state = "Online"
port_type = "NPort (fabric via point-to-point)"
speed = "8 Gbit"
supported_classes = "Class 3"
supported_fc4s = "0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 "
supported_speeds = "2 Gbit, 4 Gbit, 8 Gbit"
tgtid_bind_type = "wwpn (World Wide Port Name)"
uevent = <store method only>
Device = "host1"
Device path = "/sys/devices/pci0000:00/0000:00:07.0/0000:0e:00.0/host1"
uevent = <store method only>
Run Code Online (Sandbox Code Playgroud)
Joh*_*024 11
substr
功能这将从每个字符串中删除第一个和最后一个字符:
$ systool -c fc_host -v | awk '/Class Device =/{host=substr($4,2,length($4)-2)}/port_state/{print host,substr($3,2,length($3)-2)}'
host1 Online
Run Code Online (Sandbox Code Playgroud)
在您开始使用的代码中,有一行
host=$4
Run Code Online (Sandbox Code Playgroud)
在修改后的代码中,它被替换为:
host=substr($4,2,length($4)-2)
Run Code Online (Sandbox Code Playgroud)
该substr
函数返回 的子字符串$4
。在这种情况下,它从第二个字符开始并延伸length($4)-2
. 因此,这包括除第一个和最后一个(双引号)之外的所有字符。
出于同样的原因,这个命令:
print host,$3)
Run Code Online (Sandbox Code Playgroud)
被替换为:
print host,substr($3,2,length($3)-2)
Run Code Online (Sandbox Code Playgroud)
gsub
功能或者,gsub
可用于删除双引号:
$ systool -c fc_host -v | awk '/Class Device =/{gsub("\"","",$4);host=$4}/port_state/{gsub("\"","",$3);print host,$3}'
host1 Online
Run Code Online (Sandbox Code Playgroud)
这与您开始使用的代码一样,但添加了两个新命令:
gsub("\"","",$4)
gsub("\"","",$3)
Run Code Online (Sandbox Code Playgroud)
gsub
做替换。在这种情况下,它替换"
为一个空字符串,实际上删除了双引号。在上面的第一行,它从$4
(主机)中删除它们,在上面的第二行,它从$3
(主机)中删除它们port_state
。
$ systool -c fc_host -v | awk -F'"' '/Class Device =/{host=$2} /port_state/{print host,$2}'
host1 Online
Run Code Online (Sandbox Code Playgroud)