Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Read in 2-column CSV, output many files based on field

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-05-2012
pxalpine pxalpine is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 12 February 2013, 12:54 PM EST
Posts: 35
Thanks: 6
Thanked 0 Times in 0 Posts
Read in 2-column CSV, output many files based on field

Is there a way to read in a two-columned CSV file, and based on the fields in 1st column, output many different files? The input/output looks something like:


Code:
input.csv:
call	Call Mom.
call	Call T-Mobile.
go	Go home.
go	Go to school.
go	Go to gas station.
play	Play music.
play	Play Beatles.


Code:
outputs 3 files:
call.xml
<value><tokens><token>Call</token><token>Mom</token></tokens></value>
<value><tokens><token>Call</token><token>T-Mobile</token></tokens></value>

go.xml
<value><tokens><token>Go</token><token>home</token></tokens></value>
<value><tokens><token>Go</token><token>to</token><token>school</token></tokens></value>
<value><tokens><token>Go</token><token>to</token><token>gas</token><token>station</token></tokens></value>

play.xml
<value><tokens><token>Play</token><token>music</token></tokens></value>
<value><tokens><token>Play</token><token>Beatles</token></tokens></value>

I'm stuck at the part of checking which items in 1st column are the same, then saving all those identical items along with their rows into a new list?? Is this possible to do with shell scripts, or would I need to use Python?
Sponsored Links
    #2  
Old Unix and Linux 12-05-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 13 January 2015, 10:34 AM EST
Location: Houston, Texas, USA
Posts: 681
Thanks: 0
Thanked 206 Times in 198 Posts
try:

Code:
while read f l
do
  printf "<value>" >> $f.xml
  for w in ${l%[.]}
  do
     printf "<token>$w</token>" >> $f.xml
  done
  printf "</value>\n" >> $f.xml
done < input

The Following User Says Thank You to rdrtx1 For This Useful Post:
pxalpine (12-05-2012)
Sponsored Links
    #3  
Old Unix and Linux 12-05-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 9 April 2015, 6:09 PM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 Posts

Code:
sort input.csv | awk ' { print $1 } ' | uniq | while read file
do
 awk -v FL=$file '$1==FL {
  for (i=2;i<=NF;i++) {
   if(i==NF) printf "%s", "<tokens>"$i"</tokens></value>\n";
   if(i==2)  printf "%s", "<value><tokens>"$i"</tokens>"
   if(i!=NF && i!=2) printf "%s", "<tokens>"$i"</tokens>";
  }
 }' input.csv > $file.xml
done

    #4  
Old Unix and Linux 12-06-2012
pxalpine pxalpine is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 12 February 2013, 12:54 PM EST
Posts: 35
Thanks: 6
Thanked 0 Times in 0 Posts
What if you wanted to add a line at the beginning and end of file?


Code:
call.xml
<head><body>
<value><tokens><token>Call</token><token>Mom</token></tokens></value>
<value><tokens><token>Call</token><token>T-Mobile</token></tokens></value>
</head></body>

Sponsored Links
    #5  
Old Unix and Linux 12-06-2012
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 9 April 2015, 6:09 PM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 Posts

Code:
sort input.csv | awk ' { print $1 } ' | uniq | while read file
do
 echo "<head><body>" > $file.xml
 awk -v FL=$file '$1==FL {
  for (i=2;i<=NF;i++) {
   if(i==NF) printf "%s", "<tokens>"$i"</tokens></value>\n";
   if(i==2)  printf "%s", "<value><tokens>"$i"</tokens>"
   if(i!=NF && i!=2) printf "%s", "<tokens>"$i"</tokens>";
  }
 }' input.csv >> $file.xml
 echo "</body></head>" >> $file.xml
done

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk read column csv and search in other csv giankan Shell Programming and Scripting 8 07-02-2012 07:36 AM
Merging CSV fields based on a common field landossa Shell Programming and Scripting 1 02-09-2012 12:02 AM
extract data in a csv file based on a certain field. GroveTuckey Shell Programming and Scripting 9 11-02-2011 10:17 PM
How to split the data based on field from csv file? somehanu Shell Programming and Scripting 8 03-31-2011 04:50 AM
Read CSV column value based on column name JohnGG Shell Programming and Scripting 3 11-20-2009 12:33 AM



All times are GMT -4. The time now is 12:54 AM.