Operating System - HP-UX
1838606 Members
2761 Online
110128 Solutions
New Discussion

Re: Easy shell scripting question

 
SOLVED
Go to solution
Sean OB_1
Honored Contributor

Easy shell scripting question

I have a file that has 15-20 thousand lines like this:

field1 field11
field222 field22222222

All fields separated by a single space.

I want to run through this script and set a var equal to each of the two fields for a record, then do something else.

EX:

var1=field1
var2=field11


Just looking for the most efficient way to do this.

TIA and points for all responses.

Sean

11 REPLIES 11
Steve Steel
Honored Contributor
Solution

Re: Easy shell scripting question

Hi


cat file|while read field1 field2 rest
do
var1=field1
var2=field2
.
.
.
done

See www.shelldorado.com for scripting

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)

Re: Easy shell scripting question

in Korn:
while read var1 var2
do
# something
done < input_file

(If you dont trust input to be nice)
You can also read as:
while THE_LINE=$(line)
do
echo $THE_LINE | read var1 var2
# something
done < input_file
Dwyane Everts_1
Honored Contributor

Re: Easy shell scripting question

Sean,

Would something like this work for you?

field1="$(cat | awk '{print $1}') | echo $field2"
field11="$(cat | awk '{print $11}') | echo $field11"



for field1 in `$(cat | awk '{print $1}')`
do
for field11 in `$(cat | awk '{print $11}')`
do
echo $field1 $field11

done
done

Just a couple of ideas. If I had a sample file to test with, I couple probably come up with something a bit more extravagant :)

Dwyane
Dwyane Everts_1
Honored Contributor

Re: Easy shell scripting question

Ooooppppssss!!!! A correction

field1="$(cat | awk '{print $1}')"
echo $field1
field11="$(cat | awk '{print $11}')"
echo $field11"

got ahead of myself a little bit :)

Dwyane
Franky_1
Respected Contributor

Re: Easy shell scripting question

Hi,

num=1
cat |while read a b
do
echo var$num=$a
let num="num + 1"
echo var$num=$b
done

...assuming that you want the var_num ascending

Regards

Franky
Don't worry be happy
Geoff Wild
Honored Contributor

Re: Easy shell scripting question

will this work:

cat yourfile | while read line
do
read var1 var2 rest
echo "$var1 $var2"
done



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Stefan Schulz
Honored Contributor

Re: Easy shell scripting question

Hi,

i think a while read should be the most efficient way to do this.

Something like:

while read var1 var2
do
# do something here
done < yourinputfile


This will run through all lines of yourinputfile and for every line you can do something and use var1 for the first field and var2 for the second field. As in your example in the first loop var1 would be "field1" and var2 would be "field11" (without the " of course). In the second loop var1=field222 and var2=field22222222 and so on.

Hope this helps.

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Stefan Schulz
Honored Contributor

Re: Easy shell scripting question

Aahhrg 7 answers bevor mine. The Internet connecto to and from Germany seems to show me the answers of the others only when i post one myselfe.

Never mind.

Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Muthukumar_5
Honored Contributor

Re: Easy shell scripting question

We can do as,

while read line; do

var1=$(echo $line | awk '{ print $1 }')
var2=$(echo $line | awk '{ print $2 }')

done <
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Easy shell scripting question

There is a problem that,

if you store first field and second filed on var1 , var2 only last record's information only available there.

So we can avoid this with array simply as,

# Array index
index=0

while read line; do

var1[$index]=$(echo $line | awk '{ print $1 }')
var2[$index]=$(echo $line | awk '{ print $2 }')

let index=index+1

done <

so that

for first record it is saved as,

echo ${var1[1]} ${var2[1]}

and nth record as,

echo ${var1[n]} ${var2[n]}

HTH.

Easy to suggest when don't know about the problem!
Franky_1
Respected Contributor

Re: Easy shell scripting question

Hi Sean,

sorry i've missed one line :-(

num=1
cat |while read a b
do
echo var$num=$a
let num="num + 1"
echo var$num=$b
let num="num + 1"
done

Regards

Franky
Don't worry be happy