Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to get Columns from a file

Status
Not open for further replies.

kumariaru

IS-IT--Management
Mar 9, 2006
61
AU
Hi All,
I have a file which as three columns in it. This is a colukmn delimited file.If I open my file the data lookks something like this.
"J","566788","12-07-1890"
I want to get the column value separetly all together in a script.
something like status=J
id=566788
date=12-07-1890
I am writing put this in a script.I am new to shell scripting.So ,any help in the script is appreciated.

Thank u
Kumariaru
 
Hi

Are there multiple lines, or only one ? If just one line, you can use [tt]cut[/tt] and [tt]tr[/tt]. Is ugly, but efficient :
Code:
status=`cut -d , -f 1 | tr -d '"'`
id=`cut -d , -f 2 | tr -d '"'`
date=`cut -d , -f 3 | tr -d '"'`
If you have to process more lines one by one, then tell us abit more about the circumstances.

Feherke.
 
There's no reason to eliminate the double quotes. The shell itself can parse the data:

Code:
#!/bin/ksh

while IFS="," read col1 col2 col3
do
 eval "J=$col1"
 eval "id=$col2"
 eval "date=$col3"

 echo $J
 echo $id
 echo $date
done < myfile3
 
Or more simply...
Code:
#!/bin/ksh

while IFS="," read status id date
do
   echo $status
   echo $id
   echo $date
done < myfile3

[smile]
 
Sam:

On my Solaris 7 box, your method leaves the double quotes in the variable.
 
Oops! Sorry! didn't see the requirement to get rid of the quotes. My bad!

[smile]

Just to be ornery, how about...
Code:
#!/bin/ksh

while IFS="," read status id date
do
   echo $status
   echo $id
   echo $date
done < myfile3 | tr -d '"'
 
Sam:

Sure that works, but, arguably, mine is more fun -)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top