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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-23-2009, 07:36 AM
|
#1
|
|
Member
Registered: Nov 2009
Posts: 31
Rep:
|
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.
|
|
|
|
11-23-2009, 09:19 AM
|
#2
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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.
|
|
|
|
11-23-2009, 11:17 AM
|
#3
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Cancel what I said in your duplicate thread---this is in the right place....
|
|
|
|
11-23-2009, 01:47 PM
|
#4
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
Hi -
Bigearsbilly is correct; it's what I tried to say yesterday:
There are several different ways you can go; the example above is just one alternative.
|
|
|
|
11-24-2009, 02:39 AM
|
#5
|
|
Member
Registered: Nov 2009
Posts: 31
Original Poster
Rep:
|
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.
|
|
|
|
11-24-2009, 07:47 AM
|
#7
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by Phier
|
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 ...
|
|
|
|
11-24-2009, 08:11 AM
|
#8
|
|
Member
Registered: Nov 2009
Posts: 31
Original Poster
Rep:
|
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.
|
|
|
|
11-24-2009, 08:30 AM
|
#9
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by Phier
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)"
|
|
|
|
11-24-2009, 08:54 AM
|
#10
|
|
Member
Registered: Nov 2009
Posts: 31
Original Poster
Rep:
|
Legend!
I saw the quotes in the article linked above too and thought nothing of them at the time.
Thanks.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:48 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|