Reputation: 3
any idea how I can remove all the formatting from these responses and preserve only the 'values' portion separated by comma? I've achieved something similar but doing multiple runs and separate scripts.
response:
{
type => 'query'
timestamp => '1444304880'
serial => '0000012970'
address => '192.168.1.1'
profile => 'common'
query-id => '001'
flags => '(NET, CORP)'
version => '1.0.0.3'
}
response:
{
type => 'query'
timestamp => '1444305643'
serial => '0000012971'
address => '192.168.1.2'
profile => 'common'
query-id => '002'
flags => '(CORP)'
version => '1.0.0.3'
}
Ideal output:
query, 1444304880, 0000012970, 192.168.1.1, common, 001, (NET, CORP), 1.0.0.3
query, 1444305643, 0000012971, 192.168.1.2, common, 002, (CORP), 1.0.0.3
I noticed I can go two ways, first one is simply print out the $3 and $4 columns: awk '{print $3, $4}' dump.txt That gives me:
'query'
'1444304880'
'0000012970'
'192.168.1.1'
But it also includes the spaces created by the '{ }' which I can eliminate. My other option is to go this way and take out the response {} section.
sed "s/\'//g" dump.txt | awk '/\{/{flag=1;next}/\}/{flag=0}flag'
But then I have to bring each line up using:
sed -e '/type/{N;s/\n//;}'
Any help in doing this in a nicer way is appreciated.
Upvotes: 0
Views: 53
Reputation: 8174
Another solution, using gnu-awk
and FPAT
awk -vFPAT="['][^\n]+[']" -vRS="{" -vOFS="," '
NR>1{$1=$1; gsub(/\x27/,""); print}' file
you get,
query,1444304880,0000012970,192.168.1.1,common,001,(NET, CORP),1.0.0.3 query,1444305643,0000012971,192.168.1.2,common,002,(CORP),1.0.0.3
Upvotes: 0
Reputation: 247022
awk: uses "=>" as the field separator
awk -F "=>" '
# a line with 2 fields, remove single quotes and print with a comma
NF == 2 {gsub(/\x27/, "", $2); printf "%s,", $2}
# end of record, overwrite the trailing command and add a newline
$0 == "}" {printf "\b \n"}
' file
Upvotes: 0
Reputation: 88766
With GNU grep and paste:
grep -Po "=> '\K.*(?=')" file | paste -d , - - - - - - - -
Output:
query,1444304880,0000012970,192.168.1.1,common,001,(NET, CORP),1.0.0.3 query,1444305643,0000012971,192.168.1.2,common,002,(CORP),1.0.0.3
Upvotes: 2
Reputation: 2226
Assuming that all your input is structured as in your example, this should work:
cut -d '>' -f 2 foo.txt | grep "^ " | paste -d, - - - - - - - - | tr -d "'" | sed 's/^ //'
Upvotes: 0