In the awk
below I am trying to parse the Sample Name below the [Data] section. The values that are extracted are read into array s
(each value in a row seperated by a space) which will be used later in a bash
script. The awk
does execute but no values are printed. I am also not sure how to print in a row each value seperated by a space.
Maybe pipe the output to paste -s -d, -
to put a space in-place of a comma in k? Thank you :).
awk
s=$(awk -F'.' -v section="[Data]" -v k="Sample Name" '
$0==section{ f=1; next } ## flag when section is found in file
/\[/{ f=0; next } ## # For any lines with [ disable the flag
f && $2==k{ print $0 })' file.csv ## # If flag is set and first field is the key print k=value
file.csv
[Settings]
Adapter AGATCGGAAGAGCACACGTCTGAACTCCAGTCA
AdapterRead2 AGATCGGAAGAGCGTCGTGTAGGGAAAGAGTGT
[Data]
Sample_ID Sample_Name Sample_Plate
Sample_1 19-0000-Last-First
Sample_2 19-0001-La-Fi
Sample_3 19-0003-L-F
Sample_4 19-0004-Las-Fir
desired output ---- contents of s ----
19-0000-Last-First 19-0001-La-Fi 19-0003-L-F 19-0004-Las-Fir