Need help with a script to process a CSV file using SED and AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with a script to process a CSV file using SED and AWK
# 1  
Old 11-24-2009
Need help with a script to process a CSV file using SED and AWK

I get a CSV file every day with 2 columns and multiple rows
ex:
date1,date2 ( both the fields are varchar fields)
This data has to be updated in a table which is being done manually and i want to automate that.
1. I have to select all the data from the prod table( 2 columns { date1,date2}) into a staging table.
2. after that i have to insert the values from the CSV file into the table
3. run some validation procedures and then copy this data into prod table.
environment : sybase
OS: AIX(ksh)
I am having trouble getting data from CSV into to sybase table.
I tried to format the CSV file using SED and AWK with each column seperated by tab space and line seperated by newline.
"sed '1d' dates.txt | awk -F "," '{printf "%s\t%s\n",$1,$2}' > trial.txt"
but that gives me an when I bcp data into table. something to do with format.
any one has any idea what the right format to insert into 2 varchar columns?
or is there any other better way of doing this?

---------- Post updated at 12:02 PM ---------- Previous update was at 11:16 AM ----------

Both the fields are datetime fields.

I was thinking about storing the fields into variables and inserting these into the table using a loop.

any ideas about that ?
# 2  
Old 11-24-2009
Firstly you need to put any code between code tags as this makes your post more readable.

Secondly you need to be specific if you want help: -

"but that gives me an when I bcp data into table. something to do with format."

I assume the missing word here is error, in which case the error message / code (between code tags) would be more helpful than "something to do with format"

Good luck
# 3  
Old 11-24-2009
@steadyonabix

I was in a hurry and didnt pay much attention to formatting.

I found a temporary work around (I think it might be permanent solution).
earlier I converted the excel file to csv manually by copying them to a text file and seperating them by ','

This time i converted the excel to CSV by saving it as CSV and my script worked fine.

--------------------------------------
but I tried to build a script which inserts the values into the table reading the text file line by line.( I have pasted it below)

---------------------------------------------------
text.txt
----------------------------------------------------
1/1/2009,12/30/2008
2/1/2009,1/31/2009
3/1/2009,2/28/2009

TABLE :
----------------------------------------------------
has 2 columns { A (datetime),B(datetime)}

My Script
---------------------------------------------------
while read line
echo ${line}

a= `awk -F "," '{print$1} ${line}`( i kow i cant specify ${line} here- just wanted to tell you that i want to read line by line)
b= `awk -F "," '{print$2} ${line}`

isql -U${USER_NAME} -D${DB} -b -w400 -A8000<<!
${PASSWORD}
insert into TABLE values(${a},${b})
go
!
done < text.txt


i know that i have done a basic mistake specifying ${line} where AWK tries to find a file named by ${line} and scolds me.

I want your help to build something that reads the text file line by line and inserts those values into table after every line until end of file


Thanks in advance.
# 4  
Old 11-24-2009
Code:
a= `echo ${line}|awk -F "," '{print $1}`
b= `echo ${line}|awk -F "," '{print $2}`

# 5  
Old 11-24-2009
Quote:
Originally Posted by rdcwayx
Code:
a= `echo ${line}|awk -F "," '{print $1}`
b= `echo ${line}|awk -F "," '{print $2}`

Whoof..I missed such a small link..Hopefully i wont ask such silly questions going forward..

Cheers rdcwayxSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk and sed script to create one output CSV file

Hi All , I would require your help to generate one output file after post processing of one CSV file as stated below This file is just a small cut from a big file . Big file is having 20000 lines PATTERN,pat0,pat1,pat2,pat3,pat4,pat5,pat6,pat7,pat8,pat9... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

Datestamp format 2nd change in csv file (awk or sed)

I have a csv file formatted like this: 2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07and I'm trying to change it to MM/DD/YYYY HH:MM for both occurances. I have got this: awk -F, 'NR <=1 {print;next}{"date +%d/%m/%Y\" \"%H:%m -d\""$1 "\""| getline dte;$1=dte}1' OFS="," test.csvThis... (6 Replies)
Discussion started by: say170
6 Replies

3. Shell Programming and Scripting

Combined sed+awk for lookup csv file

have written a combined sed+awk to perform a lookup operation which works but looking to enhance it. looking to match a record using any of the comma separated values + return selected fields from the record - including the field header. so: cat foo make,model,engine,trim,value... (6 Replies)
Discussion started by: jack.bauer
6 Replies

4. Shell Programming and Scripting

Replacing FQDN by hostnames in a CSV file with sed & awk

Hello, Beginning with shell scipting, I'm trying to find in a csv file, the lines where the field related to hostname is displayed as an FQDN intead the hostname. (some lines are correct) and the to correct that inside the file: Novell,11.0,UNIX Server,bscpsiws02,TxffnX1tX1HiDoyBerrzWA==... (2 Replies)
Discussion started by: Wonto
2 Replies

5. Shell Programming and Scripting

Converting txt file into CSV using awk or sed

Hello folks I have a txt file of information about journal articles from different fields. I need to convert this information into a format that is easier for computers to manipulate for some research that I'm doing on how articles are cited. The file has some header information and then details... (8 Replies)
Discussion started by: ksk
8 Replies

6. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

7. Shell Programming and Scripting

awk/sed/something else for csv file

Hi, I have a filename.csv in which there are 3 colums, ie: Name ; prefixnumber ; number root ; 020 ; 1234567 user1,2,3 ; 070 ; 7654321 What I want is to merge colum 2 and 3 that it becomes 0201234567 or even better +31201234567 so the country number is used and drop the leading 0.... (9 Replies)
Discussion started by: necron
9 Replies

8. Shell Programming and Scripting

Using awk/sed in handling csv file.

Please study the below script and the output Script: echo "Minimum ${host} ${process} response time=${min} ms" >> ${OUTDIR}/${OUTFILE}; echo "Maximum ${host} ${process} response time=${max} ms" >> ${OUTDIR}/${OUTFILE}; echo "Average ${host} ${process} response time=${avg} ms" >>... (0 Replies)
Discussion started by: ajincoep
0 Replies

9. Shell Programming and Scripting

Updating a line in a large csv file, with sed/awk?

I have an extremely large csv file that I need to search the second field, and upon matches update the last field... I can pull the line with awk.. but apparently you cant use awk to directly update the file? So im curious if I can use sed to do this... The good news is the field I want to... (5 Replies)
Discussion started by: trey85stang
5 Replies

10. Shell Programming and Scripting

Sed or awk script to remove text / or perform calculations from large CSV files

I have a large CSV files (e.g. 2 million records) and am hoping to do one of two things. I have been trying to use awk and sed but am a newbie and can't figure out how to get it to work. Any help you could offer would be greatly appreciated - I'm stuck trying to remove the colon and wildcards in... (6 Replies)
Discussion started by: metronomadic
6 Replies
Login or Register to Ask a Question