The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to store the data retrived by a select query into variables? jisha Shell Programming and Scripting 12 01-17-2008 08:45 PM
Howto capture data from rs232port andpull data into oracle database-9i automatically boss UNIX for Dummies Questions & Answers 1 09-22-2007 11:35 PM
How to store Data in a File krishna_sicsr Shell Programming and Scripting 2 06-09-2007 05:23 AM
Extracting data from each line csaha Shell Programming and Scripting 1 04-26-2006 08:49 PM
extracting info from Unix database to construct a visual diagram fusion99 UNIX for Advanced & Expert Users 0 11-29-2004 10:29 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-25-2008
mam mam is offline
Registered User
 

Join Date: Jan 2008
Posts: 6
extracting data and store in database

Hello all,
I have this text file data. The data are separated by comma in three column and space or line feed to start a new row
Code:
anderson helberg, Jane, brother
Sister ,mother,grandpa
bombay,new york, china
I would like store them in the following format.
Code:
field1                           field2                              field3
anderson helberg              Jane                               brother
bombay                         new york                         china
my problem is how to implemet the script with both line feed or single space or multiple space and comma separator to separate the rows and stores the data as shown above.
by using cut and delimiter.
Code:
anderson helberg=`echo "$text" | cut -f 1 -d','`
please anyone with help appreciated
Reply With Quote
Forum Sponsor
  #2  
Old 01-28-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Your example doesn't match the description though,,, 'space or line feed to start a new row' isn't what you show. Your example shows it starting a new row on line feed only, and treating space as part of the field.
I'll use the output (ie line feed seperation only) as the goal here.

You can cheat and just use sed to substitute commas for tabs but it won't make a very pretty layout.
Otherwise, printf would probably be the way to go here:
Code:
#!/bin/sh
printf "%10s    %10s    %10s\n" "field1" "field2" "field3"
while read line
do
    printf "%10s    %10s    %10s\n" "`echo $line | cut -d ',' -f 1`" "`echo $line | cut -d ',' -f 2`" "`echo $line | cut -d ',' -f 3`"
done
Reply With Quote
  #3  
Old 01-29-2008
manas_ranjan's Avatar
Registered User
 

Join Date: Jul 2007
Location: PUNE
Posts: 157
Can you try this out
(You have to work out in order to format properly)
echo "field1 field2 field3"
while IFS="," read a b c
do
echo "$a $b $c"
done < FileName
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0