LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2011, 01:56 AM   #1
towhidgrep
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Rep: Reputation: 0
Smile Script for parsing a text file and inserting into DB


I have a csv file like following

data1, data2, data3, data4
rdata1, rdata2, rdata3, rdata4
....
...
...
..
.
sdata1, sdata2, sdata3, sdata4

Now i have to parse data3 and data4 of every line and send as variable to a sql select command which will connect to a DB and find out two information and lastly create a new file like following

data1, data2, data3, data4, data5, data6
...
..
.

I have done the parsing portion like this
#!/bin/bash
# Set the field seperator to a newline
IFS="
"
file=$1
cat $file | while read fileline
do
col8=$(echo $fileline | cut -d',' -f8)
col9=$(echo $fileline | cut -d',' -f9)
echo $col8 $col9
done

Now i need to do the rest of the part ..can any one help me.

Thanks in advance
 
Old 06-20-2011, 02:56 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So are you saying you do not know how to write the sql query?

Also i am not sure how the code (which you should place in [code][/code] tags to make readable)
is doing what you want?? You say you want the third and fourth columns but then use cut to get fields eight and nine??

Also, setting IFS and using cat are not required at all as the while loop can already handle both:
Code:
while read -r fileline
do
    <blah>
done<"$file"
 
Old 06-20-2011, 04:30 AM   #3
towhidgrep
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Sorry Bro it should be -f3 and -f4. I was just working with another script and i copied it from there. I know the query but i dont know how to integrate the query with scripting and how to generate another csv file with some portion from first csv file and another portion that query. And I am not setting IFS also.

Thanks
 
Old 06-20-2011, 05:14 AM   #4
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
so you get something like this:

Code:
while read -r fileline
do
  col3=`echo ${fileline} | cut -d',' -f3`
  col4=`echo ${fileline} | cut -d'.' -f4`
  sql=`/path/to/sql_command_line_tool -parameters "SELECT field1,field2 FROM table1 WHERE condition"`
  col_appd1=`echo ${sql} | cut -d'separator' -f1`
  col_appd2=`echo ${sql} | cut -d'separator' -f2`
  echo "${col3},${col4},${col_appd1},${col_appd2}" >> newfile
done < $file
I'm not sure what you really want... your description of the problem is a bit vague.
 
Old 06-20-2011, 08:20 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
No need for the cut either, a combination of IFS and read can do the parsing like this (with added error trapping and a simulated SQL script):
Code:
#!/bin/bash

# Configure script environment
set -o nounset
unalias -a

# Gopher it
IFS=', '
i=0
while read -r col1 col2 col3 col4 rest
do
    (( i++ ))
    if [[ $rest != '' ]]; then
        echo "ERROR: unexpected extra data on line $i; skipping" >&2
        continue
    fi
    SQL_data=$( echo $col3.XX $col4.XX )
    echo $col1, $col2, $col3, $col4, ${SQL_data% *}, ${SQL_data#* }
done < 'LQ-887263.txt'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting into Text Files From Bash Script userLin Programming 3 03-30-2009 07:16 AM
Help with Text Parsing Script NiallC Programming 6 06-24-2006 03:28 PM
inserting/deleting characters into a text file ananthbv Programming 7 07-13-2004 11:40 PM
inserting text into a file DavidPhillips Programming 5 08-15-2003 04:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration