1825011 Members
4645 Online
109678 Solutions
New Discussion юеВ

shell script question

 
Allan Pincus
Frequent Advisor

shell script question

Hi,

I am parsing lines in a file delimited by colons.

Can someone give me a clever line in awk or POSIX that will assign $VAR1=field1 $VAR2=field2, etc?

I need to loop through each line and do work on $VAR1 and $VAR2.

I can only pass files to the cut command, not each line that I parse with my "for" statement.

I'm not that familiar with awk.

Thanks!
10 REPLIES 10
Shannon Petry
Honored Contributor

Re: shell script question

awk -F: '{var1 = $1; var2 = $2}'
man on awk will show that you can define the field delimiter with the -F option, and inside of awk you can set anything equel to anything.
If you want to get external variables, look at the awk man pages for the -v option, which is variable assignment prior to running awk.

Not really sure what you mean though by setting $VAR2=field2 and what your trying to accomplish though... perhaps telling us what these are will help get more accurate answers.
I.E. awk has $0, $1, etc... where is the $var and field coming from?

Regards,
Shannon
Microsoft. When do you want a virus today?
Jean-Luc Oudart
Honored Contributor

Re: shell script question

example /etc/passwd

awk -F':' '{ print $1}' /etc/passwd

Jean-Luc
fiat lux
A. Clay Stephenson
Acclaimed Contributor

Re: shell script question

Since you say that you are that familiar with awk, I'' use a simple example that parses your input lines and then reads them into distinct shell variables. I use 4 variables in this example:

INFILE=/tmp/myfile

awk -F':' '{print $1, $2, $3, $4}' < ${INFILE} | while read A B C D
do
echo "A=${A}"
echo "B=${B}"
echo "C=${C}"
echo "D=${D}"
done

If less than four fields are found on a given input record (a line) then only those found will have non-null values.

If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: shell script question

What about this .. (processing the /etc/passwd file for example) ..
#!/bin/ksh
IFS=:
exec 0while read -r Name PW Uid Gid Gecos Home Shell
do
VAR1=$Name
VAR2=$Pass
VAR3=$Uid
....

done
James R. Ferguson
Acclaimed Contributor

Re: shell script question

Hi Allan:

Something like:

# V=b
# echo "a:one\nb:two\nc:three"|awk -F: -v V=$V '$1~V {print $2}'

...would echo "two" from the record "b".

Regards!

...JRF...
John Palmer
Honored Contributor

Re: shell script question

Hi,

You can do this directly is the shell as follows:-

If reading directly from a file...

#!/usr/bin/sh

IFS=:
{
while read var1 var2 rest
do
...
done
} <
unset IFS

If reading from a pipe...

#!/usr/bin/sh

IFS=:
| {
while read var1 var2 rest
do
...
done
}
unset IFS

Regards,
John
Darrell Allen
Honored Contributor

Re: shell script question

awk -F: '{print $1, $2}' filename | while read var1 var2
do
echo $var1 $var2
done

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Tom Maloy
Respected Contributor

Re: shell script question

If you are not that familiar with awk, you might try perl. Using /etc/passwd as the data file,

perl -F: -nae 'print $F[0], "\n"' /etc/passwd

The "-F" sets the separator character.
Each line gets split into the F array, numbered 0..n, so instead of printing the variable out, you can modify it within the perl script.

Tom
Carpe diem!
Allan Pincus
Frequent Advisor

Re: shell script question

Darrell,

Your answer was the easiest!! Exactly what I needed.

I could have done this in Perl, but its clumsy when you have a bunch of shell commands, so I didn't use it.

Thanks everyone!!!

- Allan
Keith Clark
Valued Contributor

Re: shell script question

You can use cut or awk if you like. I used this example file (I named it /tmp/test):

oravp1:8940:221:/usr/bin/csh
vq1adm:9277:224:/usr/bin/csh
oravq1:9276:221:/usr/bin/csh
rbillard:6498:10000:/usr/bin/sh
sanman:11574:20:/usr/bin/ksh

and this script:

for lines in $(do
VAR1=`echo $lines|cut -f 1 -d:`
VAR2=`echo $lines|cut -f 2 -d:`
VAR3=`echo $lines|cut -f 3 -d:`
VAR4=`echo $lines|cut -f 4 -d:`
echo "VAR1=$VAR1"
echo "VAR2=$VAR2"
echo "VAR3=$VAR3"
echo "VAR4=$VAR4"
done

and got this output:

VAR1=oravp1
VAR2=8940
VAR3=221
VAR4=/usr/bin/csh
VAR1=vq1adm
VAR2=9277
VAR3=224
VAR4=/usr/bin/csh
VAR1=oravq1
VAR2=9276
VAR3=221
VAR4=/usr/bin/csh
VAR1=rbillard
VAR2=6498
VAR3=10000
VAR4=/usr/bin/sh
VAR1=sanman
VAR2=11574
VAR3=20
VAR4=/usr/bin/ksh

You could replace the cut -fx -d: with awk -F: '{print $x}'. Either will work.

Keit