Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| 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. |
|
 |
09-01-2010, 12:45 PM
|
#1
|
|
Member
Registered: Mar 2008
Posts: 147
Rep:
|
for loop or while loop to read the fields of a file..
I have a mytext file with month and year as two separate fields. like
mytext fil
08 2010
09 2010
10 2010
........
........
........
I want to read the values of each field i.e., month and year into an awk script. My awk script is like...
for i in `cat mytext`
do
awk '{
print "The month of '$month' is to be posted here and"
print "The year of '$year' is to be posted here."
}' awkinput >> output
done
Final Result is like this:
The month of 08 is to be posted here and
The year of 2010 is to be posted here.
The month of 09 is to be posted here and
The year of 2010 is to be posted here.
The month of 10 is to be posted here and
The year of 2010 is to be posted here.
the above for loop writing the each field in a separate line.
Please help me..
|
|
|
|
09-01-2010, 01:15 PM
|
#2
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Hi,
if this is all you want to do then you do not need a loop at all.
Code:
awk '{print "The month of "$1" is to be posted here and";print "The year of "$2" is to be posted here.";print ""}' infile > outfile
If you need to do more then you'll have to be more specific on what you are trying to achieve.
Last edited by crts; 09-01-2010 at 01:16 PM.
Reason: fixed quotes
|
|
|
|
09-01-2010, 02:01 PM
|
#3
|
|
Member
Registered: Oct 2006
Location: Slovak Republic
Distribution: Slackware 13.37, 14.0
Posts: 381
Rep:
|
I don't know if you want to do it on the command prompt, but here is the sample in bash script:
Quote:
#!/bin/bash
awk -F " " '{print "\nThe Month:"$1,"\n" "The year:"$2,"\n"}' ./test.txt
|
Here you use the -F parameter for define the field separator, so each data after a space will be a new field.
These fields are accessed by $1 $2 $3 ...
The newline is \n. I hope it helped.
Last edited by hua; 09-01-2010 at 02:03 PM.
|
|
|
|
09-02-2010, 01:42 AM
|
#4
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
Quote:
|
Here you use the -F parameter for define the field separator, so each data after a space will be a new field.
|
This is not required here as the default is whitespace.
|
|
|
|
09-02-2010, 01:33 PM
|
#5
|
|
Member
Registered: Mar 2008
Posts: 147
Original Poster
Rep:
|
Thank you for your reply. I am sorry to say that I know this how to do with awk, but i want to read the input from a file use bash. Acutally my awk script will do some calculations depending the month and year of the file(which i cating use for loop).
|
|
|
|
09-02-2010, 01:52 PM
|
#6
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Quote:
Originally Posted by visitnag
Thank you for your reply. I am sorry to say that I know this how to do with awk, but i want to read the input from a file use bash. Acutally my awk script will do some calculations depending the month and year of the file(which i cating use for loop).
|
Ok,
you should have mentioned that in your first post. Well, in this case you might want to try
Code:
while read line; do
echo "$line" | awk 'your awk commands ...'
done < infile
A for-loop is not really suitable for this task.
Last edited by crts; 09-02-2010 at 01:54 PM.
|
|
|
|
09-02-2010, 07:58 PM
|
#7
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
if you want to use bash,
Code:
exec 4<"file"
while read -r a b<&4
do
echo "Month: $a , Year: $b"
done
exec 4<&-
you do not need the awk command inside the while loop if all you are going to do is just to print those 2 statements.
If you are going to do some further processing using awk, then using purely awk to do your processing is more efficient than a while read loop, especially if your files are big in size.
If you want to use a for loop (with cat for example), you have to set IFS=$'\n'.
Last edited by ghostdog74; 09-02-2010 at 09:09 PM.
|
|
|
|
09-02-2010, 08:09 PM
|
#8
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Quote:
Originally Posted by ghostdog74
Code:
exec 4<"file"
while read -r a b
do
echo "Month: $a , Year: $b"
done
exec 4<&-
|
If you want to use file descriptors then it should be
done <&4
The OP also stated that this is just an example and that he needs awk for some calculations. Maybe it can all be done in bash; or with awk. The OP will have to provide more details on that matter.
|
|
|
|
09-02-2010, 08:40 PM
|
#9
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
So I do not wish to hijack this thread, but as I have recently doing more with descriptors, may I ask what the advantage is here of using the descriptor as
opposed to just redirecting the file directly into the loop?
|
|
|
|
09-02-2010, 08:42 PM
|
#10
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by crts
If you want to use file descriptors then it should be
done <&4
|
yes, forgot to type it in.
|
|
|
|
09-02-2010, 08:47 PM
|
#11
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by grail
So I do not wish to hijack this thread, but as I have recently doing more with descriptors, may I ask what the advantage is here of using the descriptor as
opposed to just redirecting the file directly into the loop?
|
with a file descriptor, you take control of how you are processing the lines..eg, To simulate grep -A 2 "string"
Code:
exec 4<"file"
while read -r line <&4
do
case "$line" in
*string*)
for((i=1;i<=2;i++))
do
read nextline<&4
echo $nextline
done
esac
done
exec 4<&-
Of course, you can also do that with some counters. But the idea is the same in other languages where you can seek/tell a file using a file handle. (don't think bash has seek/tell though)
Also, you can try benchmarking using file descriptors vs input redirection. you might get more insight (yes, i have done it, so i know which one runs faster  )
Last edited by ghostdog74; 09-02-2010 at 08:52 PM.
|
|
|
1 members found this post helpful.
|
| 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 12:40 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
|
|