LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Multiple variable for loop (bash) (https://www.linuxquestions.org/questions/programming-9/multiple-variable-for-loop-bash-770964/)

Phier 11-23-2009 07:36 AM

Multiple variable for loop (bash)
 
Hi all, (first post!)

I'm trying to write a little command line for loop that parses and acts on the contents of a file. The file is formatted like this:

<Unix time in seconds since 1970-01-01 00:00:00 UTC>,<one-digit-number>,<three-digit-number>

E.g:

1258747861,1,400

I want to display 1258747861 as you would if you used the command:

date --date=@1258747861 +"%F_%T"

I.e. 2009-11-20 20:11:01

Yet keep the last field (400) in-line in the output. So display:

2009-11-20_20:11:01 400

I don't want the lines containing '999' so i tried:

for entry in $(grep -v 999 Stats/ADSLStatus.csv | awk -F "," '{print $1" "$3}'); do set $entry; echo $1; done

I read that the 'set $entry' command should pass through the for loop once for each set of variables, rather than for each item, this way i hope to run date --date=@$1 +"%F_%T" and then echo $2 without further messing around with additional variable and awk.

However $1 seems to contain both the 1258747861 item and the 400 when i try to print it, and $2 is empty. I.e. the above command gives:

1258562761
220
1258724221
400

Instead of:

1258562761
1258724221

If anyone can suggest the best course of action to solve this it would be greatly appreciated. I bet it's quite straightforward, i just can't quite figure it out today.

Cheers.

bigearsbilly 11-23-2009 09:19 AM

read will split on the IFS variable (usually a space, here a comma)
Code:

#!/bin/bash

IFS=,
while read a b c;do
    echo c= $c b = $b a = $a
done

do cat infile | script
I'm sure you can work out from here.

pixellany 11-23-2009 11:17 AM

Cancel what I said in your duplicate thread---this is in the right place....

paulsm4 11-23-2009 01:47 PM

Hi -

Bigearsbilly is correct; it's what I tried to say yesterday:
Quote:


http://www.linuxquestions.org/questi...74#post3766774

Hi -

I would recommend that you just use a "while" loop instead of a a for loop.

For several different reasons.

IMHO .. PSM
There are several different ways you can go; the example above is just one alternative.

Phier 11-24-2009 02:39 AM

Great!

Is there a way to make the script alone read and grep the file before reading each line's item? Like a 'while' equivalent of for's 'from $(command)' ?

Thanks.

Phier 11-24-2009 07:34 AM

By the way, i tried

IFS=,
while read a b c
...
done <<< $(command)

As per the suggestion here:
http://linuxshellaccount.blogspot.co...output-to.html

But now IFS doesn't work...

catkin 11-24-2009 07:47 AM

Quote:

Originally Posted by Phier (Post 3767883)
By the way, i tried

IFS=,
while read a b c
...
done <<< $(command)

As per the suggestion here:
http://linuxshellaccount.blogspot.co...output-to.html

But now IFS doesn't work...

Where doesn't IFS work?

Try
Code:

IFS=',' while read a b c
...
done <<< $(command)

If that doesn't work try
Code:

IFS=','
while read a b c
do
    unset IFS
    <code assumed to have no loop iteration>
    IFS=','
done <<< $(command)

All is not lost if neither of those work ...

Phier 11-24-2009 08:11 AM

It recognises IFS as ',' but read doesn't separate the input based on that. Instead, the first variable to read into reads the whole line of output from the command and replaces the ',' characters in it with spaces. The second and third variables are left blank.

Strange, 'cause this doesn't happen when files are used as input into the loop.

catkin 11-24-2009 08:30 AM

Quote:

Originally Posted by Phier (Post 3767912)
It recognises IFS as ',' but read doesn't separate the input based on that. Instead, the first variable to read into reads the whole line of output from the command and replaces the ',' characters in it with spaces. The second and third variables are left blank.

Strange, 'cause this doesn't happen when files are used as input into the loop.

Maybe the IFS is being used to parse the output from $(command). Try changing it to "$(command)"

Phier 11-24-2009 08:54 AM

Legend!

I saw the quotes in the article linked above too and thought nothing of them at the time.

Thanks.


All times are GMT -5. The time now is 09:25 AM.