1752591 Members
2995 Online
108788 Solutions
New Discussion юеВ

Re: Query on scripting

 
SOLVED
Go to solution
yc_2
Regular Advisor

Query on scripting

Hi,

(A) If I have a file called data that contains the following:

abc
def
ghi

I can use script to perform task like:

for i in `cat $data`
do
echo $i
done

(B) What if the file contains the following:

abc 123
def 456
ghi 789

How to reference the first column data and second column data if I would like to perform the same thing as (A) in shell script?
11 REPLIES 11
Dennis Handly
Acclaimed Contributor
Solution

Re: Query on scripting

Much better to remove the evil cat in (A):
for i in $(< $data); do

>How to reference the first column data and second column data if I would like to perform the same thing as (A) in shell script?

You can use while read:
while read first second dummy; do
echo "$first $second"
done < data

"dummy" eats up any third, etc columns.
yc_2
Regular Advisor

Re: Query on scripting

Thanks
Doug O'Leary
Honored Contributor

Re: Query on scripting

Hey;

In addition, you can also do something like:

cat ${file | while read line
do
echo "${line}"
done

Note the quotes around the variable. That syntax comes in handy if your datafile has lines with one parameter, others with two or more...

Once you have the "line" in ${line}, you can then parse it out however you want.

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
yc_2
Regular Advisor

Re: Query on scripting

Where should the "}" put in cat ${file | while read line
Steven Schweda
Honored Contributor

Re: Query on scripting

> cat ${file | while read line

> Where should the "}" put in
> cat ${file | while read line

cat ${file}

Again, "cat" is not really needed. For
example:

bash$ cat sh1.sh
#!/bin/sh

(
while read line ; do
echo "${line}"
done
) < $1


bash$ ./sh1.sh sh1.sh
#!/bin/sh

(
while read line ; do
echo "${line}"
done
) < $1
James R. Ferguson
Acclaimed Contributor

Re: Query on scripting

Hi:

> Where should the "}" put in cat ${file | while read line

Doug's post had a typographical error in that the closing brace was lost. The syntax he meant to use was:

cat ${file} | while read line

Enclosing shell variables in curly braces to signal parameter substitution is a good practice to develop. The 'sh-poxix' manpages noted that, "Braces are required when parameter is followed by a letter, digit, or underscore that should not be interpreted as part of its name or when a named parameter is subscripted."

As Dennis first noted, using a 'cat' process to read a file whose input is then piped to a shell 'read' is wasting a process. The shell can do the read without another process and is thus more efficient.

Regards!

...JRF...

Re: Query on scripting

Hello,

If I may write my own version which is slightly different than Dennis' :

exec 3< $data
while read -u3 fist second dummy ; do
print "$first $second"
done
exec 3<&-

The advantages are that :

1) it shows which file you're processing since the beginning of the loop;

2) you might open several files at the same time by changing the file descriptor (exec 3< $data 4< $data2 5< $data3).

Cheers,

Jean-Philippe
yc_2
Regular Advisor

Re: Query on scripting

> exec 3< $data

What does 3 means?
Dennis Handly
Acclaimed Contributor

Re: Query on scripting

>What does 3 means?

File number 3. The next free one after stdin(0), stdout(1) and stderr(2).
(Perhaps it is better to leave well enough alone and skip the more fancy exec stuff.)

>Jean-Philippe: 1) it shows which file you're processing since the beginning of the loop

If that's important, a comment is more descriptive than fancy exec and read -u.

>2) you might open several files at the same time ... (exec 3< $data 4< $data2 5< $data3).

It might be more understandable with one per line.