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!

Splitting up a line into fields. 2

Status
Not open for further replies.

iaresean

Programmer
Mar 24, 2003
570
ZA
Hi all;

How would I split the following data into fields?

test.txt
sean;happy;dsafdsaf
sarah;sad;fdsadsaf
henry;lost;afdfdsff

I am trying a loop through the test.txt file and trying to process during each loop. Here is how far I have gotten:
Code:
infile = "test.txt"
while read value ; do
        ### Split variables here 
        
        ### Processing here
done < $infile

Thanks for any and all help.

Sean. [peace]
 
??.. Your file is split into fields alredy..
with a ; seperator.


If u mean splitting it into 3 different files with 1 field in each u can use awk.
 
Yes they are delimited by a ';' but how would I split them up into fields so that I can use them in my processing.

e.g split $line into fields (name message garbage)

Thank you.

Sean. [peace]
 
You can set the internal field separator and just use read...
[tt]
IFS=\;
infile=test.txt
while read bingo bango bongo
do
echo 1st field: $bingo
echo 2nd field: $bango
echo 3rd field: $bongo
done < $infile
unset IFS
[/tt]
 
You can also use the following syntax (tested with KSH only) :
[tt]
infile=test.txt
while IFS=';' read bingo bango bongo
do
echo 1st field: $bingo
echo 2nd field: $bango
echo 3rd field: $bongo
done < $infile
[/tt]


Jean Pierre.
 
Another variation :- allows you to reference the whole record line as well as the individual fields.
[tt]
infile=test.txt
while read line
do
echo Record: $line
echo $line | IFS=\; read bingo bango bongo
echo 1st field: $bingo
echo 2nd field: $bango
echo 3rd field: $bongo
done < $infile
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top