file as input for script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

file as input for script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-11-2004
finster's Avatar
finster finster is offline
Registered User
 
Join Date: Nov 2002
Last Activity: 14 September 2013, 10:43 PM EDT
Location: USA
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
file as input for script

how do I use a file (comma seperated) as an input for a script in bin/sh?

e.g.

I have a script that :

Input1=$1
Input2=$2
Input3=$3
Input4=$4

echo "$Input1, $Input2, $Input3,$Input4" (or some other function)

If I have a .csv file which lists many rows of input:

joe,5,john,10
pat,77,tim,88
etc... (Input1,Input2,Input3,Input4)

How do I use this file for the input of what ever I make the script do? The input will be changed frequently, so I would like to just edit the .csv file, and have the script update the info.

Thanks
Sponsored Links
    #2  
Old 11-11-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
As long as the number of fields are fixed (and there is no

whitespace in the fields), a quick script like

Code:
 #!/bin/sh

 while read line
 do
   nocommas=`echo $line | tr ',' ' '`
   set -- $nocommas
   echo "$1 $2 $3 $4"
 done < foo.csv

should do the job

If there is whitespace, then use awk

Code:
#!/bin/sh

while read line
do
  set -- `echo $line | awk -vFS=',' '{print $1,$2,$3,$4}'`
  echo "$1 $2 $3 $4"
done < foo.csv

Cheers
ZB
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Input file to bash script Johanni Shell Programming and Scripting 2 01-25-2012 05:56 PM
AWK Script to convert input file(s) to output file jason_v_brown Shell Programming and Scripting 9 08-29-2010 09:51 PM
Need a script that will read from 2 input file syahmed Shell Programming and Scripting 3 04-13-2010 12:55 PM
Need script to take input from file, match on it in file 2 and input data darkoth Shell Programming and Scripting 1 11-03-2009 10:53 AM
File as input for a script innocentspirit Shell Programming and Scripting 5 10-26-2007 12:03 AM



All times are GMT -4. The time now is 09:04 PM.