Parsing csv file and pass to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing csv file and pass to a variable
# 1  
Old 02-19-2014
Parsing csv file and pass to a variable

Hi,

Newbie here and I need some help to parse a csv file that contains fields separated by ",". What I need to achieve here is, read the 1 line file and extract 240 fields and pass to a variable and then read the next 240 fields and pass to a variable, over and over. If anyone can assist that would be great?

ie of file.txt:

Code:
0000436AB7559EE2,00004A3DB957B72A,00004A4DE7D2867E,00004C59A9EAB388,00004A7BG85252A7,00004A84U029C38C,0000A8A30B7B59D,00003A90D022DD89,00094A90D762E3CF,00007A94419A973E,00003A94419E9747,00004A2431A29750,00004A9441A58758

Many thanks.

Last edited by Don Cragun; 02-19-2014 at 08:42 PM.. Reason: Add CODE tags.
# 2  
Old 02-19-2014
Hi, guy.
May I check what is your expected:

Your file only has 1 line, there are many fields separated by "," in this line, you need to store every 240 sequential fields in a distinct variable, right ?
# 3  
Old 02-19-2014
Following up on what Lucas_0418 said: From your description and sample one line input file (containing 220 characters broken into 13 fields by 12 commas), I have no idea what 240 fields you want to extract from this one line file nor what you want the variable to contain. I also do not understand what the next 240 fields would be from this one line file.

Please provide a better explanation of what you are trying to do and show us a sample of the output you are trying to produce.
# 4  
Old 02-19-2014
Hi,

This is the script I have referencing the csv files that i'm trying to execute:

Code:
#!/bin/bash

oIds=$(</shared/data.txt)
sId=sdata_$(date +"%m%d%y_%H:%M")
cId=cdata_$(date +"%m%d%y_%H:%M")

$CURL -s -X POST -H 'Content-Type:application/xml' -H 'id_session_id:'$sId -H 'C-Id':$cId -H "SOAPAction:http://www.hostname.com/service/model/connectors/service/Process" -T - "http://app.hostname/services/soap/soap_service" << EOD > /shared/out_file
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns='http://www.hostname.com/process/model/connectors/operations/1.0'>
   <soapenv:Header/>
   <soapenv:Body>
      <ns:Process>
         <ns:connector>${objectType}</ns:connector>
         <!--1 to 500 repetitions:-->
         <ns:id>${oIds}</ns:id>
   </ns:Process>
   </soapenv:Body>
</soapenv:Envelope>
EOD

so what i'm trying to do is read the "csv/txt" file of the fields separated by "," and pass it repeatedly to "${oIds}"
# 5  
Old 02-20-2014
Try this:
Code:
while read line
do
        for oIds in ${line//,/ }
        do
                # -- your code here -- #
        done
done < file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass some data from csv to xml file using shell/python

Hello gurus, I have a csv file with bunch of datas in each column. (see attached) Now I have an .xml file in the structure of below: ?xml version="1.0" ?> <component id="root" name="root"> <component id="system" name="system"> <param name="number_of_A" value="8"/> ... (5 Replies)
Discussion started by: Zam_1234
5 Replies

2. Shell Programming and Scripting

Help with Parsing a CSV File

Hello All, I have an input CSV file like below, where first row data can be in different position after every run of the tool, i.e. pzTest in below example is in column 1, but it can be also in 3 column and same for all the headers in the first row. pzTest, pzExtract, pxUpdate, pzInfo... (1 Reply)
Discussion started by: asirohi
1 Replies

3. Shell Programming and Scripting

Csv file parsing and validating

Hi, I have basic knowledge on unix shell scripting(not an expert). My requirement is reading the csv file using the schema defined in the configuration file and if the condition is not mached then move the unmatched record to a error file and matched good records into other file. In brief: ... (43 Replies)
Discussion started by: shree11
43 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Shell Programming and Scripting

Help required in parsing a csv file

Hi Members, I am stuck with the following problem. Request your kind help I have an csv file which contains, 1 header record, data records and 1 footer record. Sample is as below Contents of cm_update_file_101010.csv -------------------------------------------------- ... (6 Replies)
Discussion started by: ramakanth_burra
6 Replies

6. Shell Programming and Scripting

Parsing complicated CSV file with sed

Yes, there is a great doc out there that discusses parsing csv files with sed, and this topic has been covered before but not enough to answer my question (unix.com forums). I'm trying to parse a CSV file that has optional quotes like the following: "Apple","Apples, are fun",3.60,4.4,"I... (3 Replies)
Discussion started by: analog999
3 Replies

7. Shell Programming and Scripting

Parsing a CSV File

Hey guys, I'm in the process of learning PHP and BASH scripting. I'm getting there, slowly ;) I would like some help with parsing a CSV file. This file contains a list of hostnames, dates, and either Valid, Expired, or Expired Soon in the last column. Basically, I want to parse the file,... (12 Replies)
Discussion started by: dzl
12 Replies

8. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

9. Shell Programming and Scripting

CSV file parsing and validation

I have a CSV file that needs to through two seperate processes (in the end there will be 2 files (Dload.unl and Tload.unl and we'll say the input file name is mass.csv). I have a processfile() function that will call the process Dload funtion. In Dload I want to read mass.csv into Dload and then... (1 Reply)
Discussion started by: dolo21taf
1 Replies

10. Shell Programming and Scripting

Parsing a csv file

I am trying to parse a csv file in the below 'name-value pair' format and then use the values corresponding to the name. Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname... (6 Replies)
Discussion started by: chiru_h
6 Replies
Login or Register to Ask a Question