Hi to all,
I have a file like that in attach, I need to create a table or csv with only this field:
"Barcode","Device Name", "ACSL".
I tried with grep with -B
options but the number of lines are different for any block is different, there is a method with sed or awk to extract it.
Thanks a lot
Best Regards
anbu23
2
Can you post expected output?
rdrtx1
3
awk -v columns="Barcode,Device Name,ACSL" '
BEGIN {
c=split(columns, headers, ",");
for (i=1; i<=c; i++) labels[headers]=headers;
}
{
for (i=1; i<=NF; i++) {
key=$i;
sub("=.*", "", key);
if (key in labels) {
key=$i;
sub("=.*", "", key);
value=$i;
sub(key "=", "", value);
data[NR, key]=value;
}
}
}
END {
print columns;
for (i=1; i<=NR; i++) {
line="";
for (j=1; j<=c; j++) {
line=line data[i, headers[j]] ",";
}
sub(",$", "", line);
if (line ~ /..*,..*,..*/) print line;
}
}
' FS="\n" RS="\n\n" file2.txt > file2.csv
# note: ex. excludes empty field lines