LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   for loop or while loop to read the fields of a file.. (http://www.linuxquestions.org/questions/linux-newbie-8/for-loop-or-while-loop-to-read-the-fields-of-a-file-829776/)

visitnag 09-01-2010 12:45 PM

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..

crts 09-01-2010 01:15 PM

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.

hua 09-01-2010 02:01 PM

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.

grail 09-02-2010 01:42 AM

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.

visitnag 09-02-2010 01:33 PM

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).

crts 09-02-2010 01:52 PM

Quote:

Originally Posted by visitnag (Post 4086247)
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.

ghostdog74 09-02-2010 07:58 PM

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'.

crts 09-02-2010 08:09 PM

Quote:

Originally Posted by ghostdog74 (Post 4086540)
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.

grail 09-02-2010 08:40 PM

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?

ghostdog74 09-02-2010 08:42 PM

Quote:

Originally Posted by crts (Post 4086549)
If you want to use file descriptors then it should be
done <&4

yes, forgot to type it in.

ghostdog74 09-02-2010 08:47 PM

Quote:

Originally Posted by grail (Post 4086566)
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 :))


All times are GMT -5. The time now is 03:57 AM.