Awk / gsub - print everything between double quotes in multiple occurrences per line

I am trying to print all data between double quotes ( sampleField="sampleValue"), but I find it hard to get awkand / or sub/ gsubto return all instances of data between double quotes. Then I would like to print all the instances in the corresponding lines that they found in order to save the data together.

Here is an example file input.txt:

deviceId="1300", deviceName="router 13", deviceLocation="Corp"
deviceId="2000", deviceName="router 20", deviceLocation="DC1"

The result I'm looking for is:

"1300", "router 13", "Corp"
"2000", "router 20", "DC1"

I'm having trouble using gsub to delete all data between ,and =. Each time I tried a different approach, it always just returns the first field and moves to the next line.

UPDATE:

, , . 1, 3 5 000. , , , .

+5
6

A sed :

sed -r 's/[^\"]*([\"][^\"]*[\"][,]?)[^\"]*/\1 /g'
    <<< 'deviceId="1300", deviceName="router 13", deviceLocation="Corp"'

:

"1300", "router 13", "Corp"

:

sed -r 's/[^\"]*([\"][^\"]*[\"][,]?)[^\"]*/\1 /g' input.txt
+5
awk -F '"' '{printf(" %c%s%c, %c%s%c, %c%s%c\n", 34,$2, 34, 34, $4,34, $6, 34) } ' \
    input file > newfile

- , .

awk 'BEGIN{ t=sprintf("%c", 34)}
     { for(i=1; i<=NF; i++){
        if(index($i,t) ){print $i}  }; printf("\n")}'  infile > outfile

awk.

+2
awk -F \" '
    {
        sep=""
        for (i=2; i<=NF; i+=2) {
            printf "%s\"%s\"", sep, $i
            sep=", "
        }
        print ""
    }
' << END
deviceId="1300", deviceName="router 13", deviceLocation="Corp", foo="bar"
deviceId="2000", deviceName="router 20", deviceLocation="DC1"
END

"1300", "router 13", "Corp", "bar"
"2000", "router 20", "DC1"
+1

awk/sub/gsub/- , , . , :

(1) Perl:

172-30-3-163:ajax vphuvan$ perl -pe 's/device.*?=//g' input.txt
"1300", "router 13", "Corp"
"2000", "router 20", "DC1"

where 
-p means "print to screen"
-e means execute the statement between the single quotes
s is a regular expression command which gives the instruction to substitute
g is the switch for the regular expression. /g instructs the program to carry out the substitution /device.*?=// wherever applicable
/device.*?=// is an instruction to replace with an empty string '' any expression that starts with the prefix "device" and that ends just before the closest "=" sign. Note that "deviceId", "deviceName"  and "deviceLocation" all start with the prefix "device" and each of them ends just before the "=" sign

(2) bash:

172-30-3-163:ajax vphuvan$ sed "s/deviceId=//; s/deviceName=//; s/deviceLocation=//" input.txt
"1300", "router 13", "Corp"
"2000", "router 20", "DC1"

sed , "deviceId", "deviceName" "deviceLocation "

, sed ( sub gsub) , Perl, . , sed, sub/gsub "?", .

+1

awk -F\" '{ for(i=2; i<=NF; i=i+2){ a = a"\""$i"\""",\t";} {print a; a="";}}' temp.txt

"1300",  "router 13",     "Corp"
"2000",  "router 20",     "DC1"
0

, :

 $ awk -F"=|," '{print $2,$4,$6}' input.txt
"1300" "router 13" "Corp"
"2000" "router 20" "DC1"
0

All Articles