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. |
read will split on the IFS variable (usually a space, here a comma)
Code:
#!/bin/bash I'm sure you can work out from here. |
Cancel what I said in your duplicate thread---this is in the right place....
|
Hi -
Bigearsbilly is correct; it's what I tried to say yesterday: Quote:
|
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. |
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... |
Quote:
Try Code:
IFS=',' while read a b c Code:
IFS=',' |
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. |
Quote:
|
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. |