awk to parse section of csv into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to parse section of csv into array
# 1  
Old 03-12-2019
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.

awk
Code:
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
Code:
[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 ----
Code:
19-0000-Last-First 19-0001-La-Fi 19-0003-L-F 19-0004-Las-Fir

# 2  
Old 03-12-2019
Code:
s=$(awk '/^Sample_[0-9]/      {print $2}' file.csv)
echo $s

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

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

Code:
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
Code:
awk -v k="Sample_Name" '
/^\[Data/               { f = 1
                          getline
                          for(i=1; i <= NF; i++) if (k == $i) break
                          next
                        }
f && /^\[/              { f = 0
                        }
f                       { print $i
                        } ' file.csv


Last edited by nezabudka; 03-12-2019 at 11:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

2. Shell Programming and Scripting

Parse csv files by their names

HI all I have multiple csv files with the names VAR1_VAR2_VAR3_VAR4.csv All the files have the same structure inside just values change. I am trying to retrieve data from those files by fixing at each time one or more VAR. I tried to write a script but I have 2 problems: 2-... (1 Reply)
Discussion started by: Jhon.c
1 Replies

3. Shell Programming and Scripting

Parse configuration file & add line in particular section

Greetings, I recently built a replicated DRBD, Heartbeat, & iSCSI Target Initiator storage server on Ubuntu 10.04 to offer shared storage to server Vmware ESX and Microsoft Clusters. Everything works flawlessly, however I wanted to make a script to create, remove, grow volumes to offer ESX... (6 Replies)
Discussion started by: Aeudian
6 Replies

4. Shell Programming and Scripting

Initializing array using awk from parsed csv

Hi all, I am fairly new to unix scripting and I am struggling with some awk commands here. I want to create an array COMPANYCODE from some parsed csv values (single column) . I have been able to access the data in other ways but I would prefer if I could put each value in it's own array... (6 Replies)
Discussion started by: P8TRO
6 Replies

5. Shell Programming and Scripting

How to parse csv format?

Hi, I have a file with 3 fields in csv format: /tmp/foo/,MODIFY,bar/toto "/tmp/foo, bar/","ATTRIB,ISDIR","toto, tata/foobar"I would like to split fields in order to obtain the following: Line1: /tmp/foo/ MODIFY bar/totoLine2: /tmp/foo, bar/ ATTRIB,ISDIR toto, tata/foobarCan't find my way... (11 Replies)
Discussion started by: chebarbudo
11 Replies

6. Shell Programming and Scripting

Parse Multi-Section Configuration File

Hello all, Sample configuration file: username = root password = admin IpAddress = 192.168.2.90 HttpCommand = /getfile?hello.jpg Username = root2 Password = admin2 Passive = no Host = 192.168.1.100 Path = /uploads Username = root3 Password = adming Passive = no Host =... (10 Replies)
Discussion started by: LAVco
10 Replies

7. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

8. Shell Programming and Scripting

Parse csv file

Hi, Our requirement is to parse the input file(.csv format). The each column in the file is delimited with comma. We need to take each column and apply some business validation rule. If data itself contains comma, then those fields are enclosed with double quotes ("). We can see this double... (7 Replies)
Discussion started by: vfrg
7 Replies

9. Shell Programming and Scripting

Unix Script to parse a CSV

I am writing a unix script that will parse a CSV and edit the values. My CSV looks like this 0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0 10,11,7,0,4,12,2,3,7,0,11,3,12,4,0,5,5,4,5,0,8,6,12,0,9,3,3,0,2,7,8... (16 Replies)
Discussion started by: RJ17
16 Replies

10. UNIX for Advanced & Expert Users

How to Parse a CSV file into a Different Format

Hi I have a CSV file with me in this format Currency, USD, EUR, USD, 1.00, 1.32, EUR, 0.66, 1.00, How do I transpose the file to get to the format below. currency, currency, rate USD, USD, 1.00 USD, EUR, 1.32 EUR, USD, 0.66 EUR, EUR, 1.00 Thanks for your help We are using... (2 Replies)
Discussion started by: cdesiks
2 Replies
Login or Register to Ask a Question