Remove Quotes (double or single) with Awk

awk

I want to remove quotes(double quotes) appearing in the awk output, how it can be achieved

 # 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"

Below is the Command & command output..

# 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>

Best Answer

Using awk's substr function

This removes the first and last characters from each string:

$ 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

How it works:

In the code that you started with, there was the line

host=$4

In the revised code, that is replaced with:

host=substr($4,2,length($4)-2)

The substr function returns a substring of $4. In this case, it starts from the second character and extends a length of length($4)-2. Thus, this includes all characters except the first and last (which are the double quotes).

For the same reason, this command:

print host,$3)

was replaced with:

print host,substr($3,2,length($3)-2)

Using GNU awk's gsub function

Alternatively, gsub can be used to remove the double quotes:

$ systool -c fc_host -v | awk '/Class Device =/{gsub("\"","",$4);host=$4}/port_state/{gsub("\"","",$3);print host,$3}'
host1 Online

How it works

This is again just like the code that you started with but with two new commands added:

gsub("\"","",$4)
gsub("\"","",$3)

gsub does substitutions. In this case, it replaces " with an empty string, in effect removing the double quotes. On the first line above, it removes them from $4 (which is the host) and on the second line above, it removes them from $3 (which is the port_state).

Using awk's Field Separator

$ systool -c fc_host -v | awk -F'"' '/Class Device =/{host=$2} /port_state/{print host,$2}'
host1 Online
Related Question