Operating System - HP-UX
1834704 Members
2655 Online
110069 Solutions
New Discussion

Re: How to read each line into a variable and do some operation - Shell Script Question ??

 
SOLVED
Go to solution
Praveen Hari
Advisor

How to read each line into a variable and do some operation - Shell Script Question ??

Hi,

I have file in the format
xxx1 100 120
xxx2 230 250
xxx3 200 300

I want to go through each line of this file and read 3 words into 3 different varaibales uri, start_item and end_item. I need to do this in a loop and I have another script which takes these 3 varaiables and creates another output for each line.
How can I do this within a loop ?
Can some Shell/Awk Guru help me ?
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to read each line into a variable and do some operation - Shell Script Question ??

INFILE=myfile

cat ${INFILE} | while read A B C
do
echo "A = ${A} B = ${B} C = ${C}"
done
If it ain't broke, I can fix that.
Tim Adamson_1
Honored Contributor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

I would have thought that Clay's answer would assign A=xxx1, B=100 and C=120.

Wouldn't 3 separate read lines be required?


Tim.
Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
Tim Adamson_1
Honored Contributor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

Misinterpreted the question. Sorry Clay :p

Tim.
Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
Praveen Hari
Advisor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

I have another related question:
Now that I can read all 3 into variables A, B and C.

I want to concatenate some string to variable A.
e.g.
infile=test.txt
cat $infile | while read A B C
do
filename=$A + ".bif"
echo "$A $B $C $filename
done

Can I do something like that ? It doesn't work ?
How do I concatenate a string ".bif" to $A for each line while I am doing the loop.
Thanks
Rob_132
Regular Advisor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

Is this what you were trying to do?

>outfile
infile=test.txt
cat $infile | while read A B C
do
echo $A $B $C $A.bif >>outfile
done

Rob
Rob_132
Regular Advisor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

On re-reading, maybe this is what you wanted.

infile=test.txt
cat $infile | while read A B C
do
echo $A $B $C >>$A.bif
done

Rob
Praveen Hari
Advisor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

Hi All,

This is what I want:
I have a file1 with following content:
xxx1 100 105
xxx2 200 210
xxx3 150 175

For each line of this file, I need to genearte a new file with contenats and file name
in the pattern:

file: xxx1.bif
http://yyy.com/test.cgi?cmd=item-100&group=xxx1
http://yyy.com/test.cgi?cmd=item-101&group=xxx1
..
..
http://yyy.com/test.cgi?cmd=item-105&group=xxx1



file: xxx2.bif
http://yyy.com/test.cgi?cmd=item-200&group=xxx2
http://yyy.com/test.cgi?cmd=item-201&group=xxx2
..
..
http://yyy.com/test.cgi?cmd=item-210&group=xxx2


I am trying following script and it doesn't work:

infile=test.txt
url="http://yyy.com/test.cgi?cmd=item-"
uri="group="
suffix=".bif"

cat $infile | while read name start end
do
filename=`expr $name$suffix`
if [$end -gt $start]
then
while [$end -ge $start]
do
echo $url$end$uri$name >> $filename
echo "<>" >> $filename
end=`expr $end-1`
done

done



Any help is appreciated. The inner loop fails.
Rob_132
Regular Advisor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

Your requirements are a moving target.

Maybe you could ask the entire question the first time(??).

http://us-support.external.hp.com/estaff/bin/doc.pl/screen=estaffAssistance/distrib_redir=0+1062041015|*?Page=file0002
Rob_132
Regular Advisor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

Praveen Hari
Advisor

Re: How to read each line into a variable and do some operation - Shell Script Question ??

I have posetd entire question again with title:
Shell Scripting Help .. Please Help