LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-23-2009, 08:36 AM   #1
Phier
Member
 
Registered: Nov 2009
Posts: 31

Rep: Reputation: 0
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.
 
Old 11-23-2009, 10:19 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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.
 
Old 11-23-2009, 12:17 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Cancel what I said in your duplicate thread---this is in the right place....
 
Old 11-23-2009, 02:47 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 11-24-2009, 03:39 AM   #5
Phier
Member
 
Registered: Nov 2009
Posts: 31

Original Poster
Rep: Reputation: 0
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.
 
Old 11-24-2009, 08:34 AM   #6
Phier
Member
 
Registered: Nov 2009
Posts: 31

Original Poster
Rep: Reputation: 0
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...
 
Old 11-24-2009, 08:47 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by Phier View Post
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 ...
 
Old 11-24-2009, 09:11 AM   #8
Phier
Member
 
Registered: Nov 2009
Posts: 31

Original Poster
Rep: Reputation: 0
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.
 
Old 11-24-2009, 09:30 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by Phier View Post
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)"
 
Old 11-24-2009, 09:54 AM   #10
Phier
Member
 
Registered: Nov 2009
Posts: 31

Original Poster
Rep: Reputation: 0
Legend!

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

Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a variable to control a BASH for loop JohnE1 Programming 15 07-04-2009 04:37 AM
BASH hangman: loop which adds the character to the variable if it matches uzi85 Programming 3 03-28-2009 10:31 AM
bash - loop over variable array names talanis Programming 2 02-19-2009 12:09 PM
bash/sh: global or local variable with for (loop) isssue frenchn00b Programming 9 11-06-2008 08:24 AM
define multiple variable through loop in makefiles sun_sun Programming 1 11-28-2007 09:16 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration