awk to parse section of csv into array

Login or Register to Reply

Thread Tools Search this Thread
# 1  
Old 1 Week Ago
awk to parse section of csv into array

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 Smilie.

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

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

# 2  
Old 1 Week Ago
s=$(awk '/^Sample_[0-9]/      {print $2}' file.csv)
echo $s

but it is not array. The array is like this
s=($(awk '/^Sample_[0-9]/      {print $2}' file.csv))
echo ${s[@]}

--- Post updated at 04:50 ---

awk '
/^\[Data/               { f = 1 }
f && /^\[/              { f = 0 }
f && /^Sample_[0-9]/    { print $2 }' file.csv

--- Post updated at 05:19 ---

I certainly don’t know the complexity of your CSV file. But the algorithm that you want to implement looks like so in my opinion
awk -v k="Sample_Name" '
/^\[Data/               { f = 1
                          for(i=1; i <= NF; i++) if (k == $i) break
f && /^\[/              { f = 0
f                       { print $i
                        } ' file.csv

Last edited by nezabudka; 1 Week Ago at 10:25 PM..
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
How to parse this file using awk and output in CSV format? Balav Shell Programming and Scripting 10 11-23-2015 12:48 PM
Parse csv files by their names Jhon.c Shell Programming and Scripting 1 10-10-2013 09:21 PM
Help to parse csv file with shell script Grhyll UNIX for Dummies Questions & Answers 4 01-19-2012 09:45 AM
how to parse this file and obtain a .csv or .xls Ant-one Shell Programming and Scripting 17 04-05-2011 12:39 PM
Parse configuration file & add line in particular section Aeudian Shell Programming and Scripting 6 03-22-2011 02:59 PM
Initializing array using awk from parsed csv P8TRO Shell Programming and Scripting 6 12-05-2010 09:06 PM
Extract section of file based on word in section jelloir Shell Programming and Scripting 2 09-20-2010 02:16 AM
How to parse csv format? chebarbudo Shell Programming and Scripting 11 07-08-2010 07:47 AM
Parse Multi-Section Configuration File LAVco Shell Programming and Scripting 10 02-28-2010 04:50 PM
Need to parse file "x" lines at a time ... awk array? STN Shell Programming and Scripting 7 01-27-2010 03:51 PM
Parse csv file vfrg Shell Programming and Scripting 7 08-19-2009 11:07 PM
Parse XML file into CSV with shell? Pcushing Shell Programming and Scripting 7 04-14-2009 10:27 PM
Unix Script to parse a CSV RJ17 Shell Programming and Scripting 16 09-10-2008 10:07 PM
CSV File parse help in Perl lodey Shell Programming and Scripting 13 03-31-2008 09:06 AM
How to Parse a CSV file into a Different Format cdesiks UNIX for Advanced & Expert Users 2 11-04-2005 08:11 AM