LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 09-01-2010, 12:45 PM   #1
visitnag
Member
 
Registered: Mar 2008
Posts: 147

Rep: Reputation: 15
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..
 
Old 09-01-2010, 01:15 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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
 
Old 09-01-2010, 02:01 PM   #3
hua
Member
 
Registered: Oct 2006
Location: Slovak Republic
Distribution: Slackware 14.2, current
Posts: 461

Rep: Reputation: 78
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.
 
Old 09-02-2010, 01:42 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 09-02-2010, 01:33 PM   #5
visitnag
Member
 
Registered: Mar 2008
Posts: 147

Original Poster
Rep: Reputation: 15
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).
 
Old 09-02-2010, 01:52 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by visitnag View Post
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.
 
Old 09-02-2010, 07:58 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 09-02-2010, 08:09 PM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by ghostdog74 View Post
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.
 
Old 09-02-2010, 08:40 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 09-02-2010, 08:42 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by crts View Post
If you want to use file descriptors then it should be
done <&4
yes, forgot to type it in.
 
Old 09-02-2010, 08:47 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by grail View Post
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.
  


Reply



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 12:06 PM
awk: swapping fields and records and for loop sebelk Programming 5 05-10-2010 07:41 PM
[SOLVED] Trying to read a file with a while loop. sinderone Programming 5 04-26-2010 02:37 AM
Reg Makefile : Read file : for loop mogra Linux - Newbie 2 03-16-2008 12:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:09 PM.

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
Open Source Consulting | Domain Registration