Reputation: 43518
I have a file containing the following data:
toto,1,xsd:integer
titi,true,xsd:boolean
tata,str,attr,xsd:string
infact the format of each line is:
parameter_name,value,...,xsd:type
the parameter_name is fix and it's in the position 1. The value is also fix and it's in position 2. But the type position is variable it could at any postion except the position 2 and 1. but the type field contains always the prefix xsd
.
Now I m trying to execute an awk to extract only the param name, the value and the type. For the moment I m able to extract the param name and the value with
awk -F"," '{print $1"-"$2}'
But I m not able to extract the type field since its position is variable. How I can get the type field based on its prefix xsd
in the awk
output?
Upvotes: 1
Views: 1560
Reputation: 780779
Loop over the rest of the fields looking for the prefix.
awk -F, '{type="";
for (i = 3; i <= NF; i++) { if($i ~ /^xsd:/) { type = $i; break; } }
print $1"-"$2"-"type; }'
Upvotes: 2
Reputation: 174696
You could try this GNU sed command,
$ sed -r 's/^([^,]*),([^,]*),.*(xsd:[^,]*).*/\1,\2,\3/g' file
toto,1,xsd:integer
titi,true,xsd:boolean
tata,str,xsd:string
OR
$ sed -r 's/^([^,]*),([^,]*),.*(xsd:[^,]*).*/\1-\2-\3/g' file
toto-1-xsd:integer
titi-true-xsd:boolean
tata-str-xsd:string
Upvotes: 0