how do i process each line in a text file until it reaches EOF? 
Author Message
 how do i process each line in a text file until it reaches EOF?

basically i have a text file containing ... say a listing of files in a
certain directory as in:

-rw-rw-rw-   1 test       users         2349 May 31 11:44
CRE.OUT.000531.113933
-rw-rw-rw-   1 test       users          521 May 31 11:44
CRE.OUT.000531.113934
-rw-rw-rw-   1 test       users         5541 May 31 14:01
CRE.OUT.000531.133945
-rw-rw-rw-   1 test       users         1026 May 31 14:01
CRE.OUT.000531.133946
-rw-r--r--   1 test       users          335 May 31 14:55
CRE.OUT.000531.144925
-rw-r--r--   1 test       users          352 May 31 14:55
CRE.OUT.000531.144926

now i want to capture the filename and process each file (in this case
6 files).

now i know that i can use awk as in awk 'NR==3' input_file

but that specifies a specific record. i want it to automatically go
through the entire textfile picking up each line until it reaches EOF.
does awk have a predefined variable for EOF?

how can i do this?

thanks in advance!

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 17 Nov 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?

Quote:

>basically i have a text file containing ... say a listing of files in a
>certain directory as in:

>-rw-rw-rw-   1 test       users         2349 May 31 11:44
>CRE.OUT.000531.113933
>-rw-rw-rw-   1 test       users          521 May 31 11:44
>CRE.OUT.000531.113934
>-rw-rw-rw-   1 test       users         5541 May 31 14:01
>CRE.OUT.000531.133945
>-rw-rw-rw-   1 test       users         1026 May 31 14:01
>CRE.OUT.000531.133946
>-rw-r--r--   1 test       users          335 May 31 14:55
>CRE.OUT.000531.144925
>-rw-r--r--   1 test       users          352 May 31 14:55
>CRE.OUT.000531.144926

>now i want to capture the filename and process each file (in this case
>6 files).

>now i know that i can use awk as in awk 'NR==3' input_file

>but that specifies a specific record. i want it to automatically go
>through the entire textfile picking up each line until it reaches EOF.
>does awk have a predefined variable for EOF?

>how can i do this?

awk normally processes each line, comparing the pattern, and if
the pattern matches, then the action is performed.

If there is no pattern, then the action is performed on each line.

e.g.

awk '{print $NF}' infile

prints the last field for each record.

As there is no pattern, the action (in curly braces) is performed
for each record.

If there is no action, then it is  assumed that you want to print
the record.

A feature of awk (and sed) is that there is an implied looping
through all records in the input.  This can be modified by other
statements, such as exit, but that's picking nits.

Chuck Demas
Needham, Ma.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 18 Nov 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?

Quote:

>basically i have a text file containing ... say a listing of files in a
>certain directory as in:

>-rw-rw-rw-   1 test       users         2349 May 31 11:44
>CRE.OUT.000531.113933
>-rw-rw-rw-   1 test       users          521 May 31 11:44
>CRE.OUT.000531.113934
>-rw-rw-rw-   1 test       users         5541 May 31 14:01
>CRE.OUT.000531.133945
>-rw-rw-rw-   1 test       users         1026 May 31 14:01
>CRE.OUT.000531.133946
>-rw-r--r--   1 test       users          335 May 31 14:55
>CRE.OUT.000531.144925
>-rw-r--r--   1 test       users          352 May 31 14:55
>CRE.OUT.000531.144926

>now i want to capture the filename and process each file (in this case
>6 files).

>now i know that i can use awk as in awk 'NR==3' input_file

>but that specifies a specific record. i want it to automatically go
>through the entire textfile picking up each line until it reaches EOF.
>does awk have a predefined variable for EOF?

>how can i do this?

>thanks in advance!

Duane --

If I understand your question, you want to scan the output
of ls -l and then for each filename in the ls -l list, scan
the filename in the listing.

Here is one way:

################# cut here ################
BEGIN {

   NumFld   = 9

Quote:
}

function ScanFile( InFile,      NumRec )
{
   # Cats InFile and count Records.
   # Prints Total Records
   # Returns Total Records

   NumRec = 0

   while (( getline < InFile ) > 0 )
   {
      # do something with the file
      # here is a simple `cat` command
      print
   }

   # Here is where the EOF() processing goes
   print "# Number of Records = " NumRec

   return ( NumRec )

Quote:
}

# main awk loop
{
   if ( NF >= NumFld )
      N = ScanFile( $(NF) )

Quote:
}

################# cut here ################

hth.

-- kjh

p.s.  I keep a generic version of an ls -la scaner in
my ~/bin directory.  It is a handy utility for processing
lists of files ( the long output is easier to edit for
me because there is more information like date, size, etc )

Here is a script I call `llname`

################ cut here #################
#!/bin/sh

gawk '
{
   if ( NF >= 9 )
   {
      # the following if (()&&()) is only necessary
      # for a belt-n-suspenders kinda guy ;-)

      if (( $(NF) != "." ) && ( $(NF) != ".." ))
         print $(NF)
   }
################ cut here #################

It is handy in shell constructs like:

   for file in `llname foo`
   do
      # something
   done
--
------------------------------------------------------------

1111 Seacoast Dr.  Unit 41   |  home:   (619) 423-4451     |
Imperial Beach, CA   91932   |                             |



Thu, 21 Nov 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?
Hi,
I am trying to use getline to read first line from the text file:

awk '{getline first<"test.txt"}; {print first}; {close("test.txt")}'

However, awk still seems  to expect filename from the standard input.
Can someone tell me where is the error here?
Thank you,
Victor L.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 07 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?

Quote:

>Hi,
>I am trying to use getline to read first line from the text file:

>awk '{getline first<"test.txt"}; {print first}; {close("test.txt")}'

>However, awk still seems  to expect filename from the standard input.
>Can someone tell me where is the error here?

Don't use getline if you can possibly avoid it.  I've been using AWK for
(mumble, mumble) years now, and even now, whenever I code an algorithm that
uses getline, I almost always later determine it to be bad design.

In you case:

        awk '{print;exit}' test.txt



Sat, 07 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?

Quote:

>Hi,
>I am trying to use getline to read first line from the text file:

>awk '{getline first<"test.txt"}; {print first}; {close("test.txt")}'

>However, awk still seems  to expect filename from the standard input.
>Can someone tell me where is the error here?

You're not teling awk what to operate on, you're specifying a command
and not giving it the thing the command is to operate upon.

Awk expects to deal with _SOME_ input, so give it some, either an
existing file, or something from standard input like this:

echo "a" | awk '{getline first<"test.txt"};
                {print first}; {close("test.txt")}'

Note that if you used this with a file input, it would output the
first line of test.txt for each line in your input file.

Better would be:

echo "a" | awk 'BEGIN{getline first<"test.txt"
                print first
                close("test.txt")}'

or

awk 'BEGIN{getline first<"test.txt"
           print first
           close("test.txt")}' infile

Where infile is any multiline file, and this awk code would only
print the first line of test.txt once.

I assume you're aware that there are easier ways to read and print
the first line of a file.  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 07 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?

Quote:

> >Hi,
> >I am trying to use getline to read first line from the text file:

> >awk '{getline first<"test.txt"}; {print first}; {close("test.txt")}'

> >However, awk still seems  to expect filename from the standard input.
> >Can someone tell me where is the error here?

> Don't use getline if you can possibly avoid it.  I've been using AWK for
> (mumble, mumble) years now, and even now, whenever I code an algorithm
that
> uses getline, I almost always later determine it to be bad design.

> In you case:

> awk '{print;exit}' test.txt

Am I missing something or shouldn't you just be using 'head -1
inputfilename'

rgds
steve



Sat, 07 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?

Quote:




>> >Hi,
>> >I am trying to use getline to read first line from the text file:

>> >awk '{getline first<"test.txt"}; {print first}; {close("test.txt")}'

>> >However, awk still seems  to expect filename from the standard input.
>> >Can someone tell me where is the error here?

>> Don't use getline if you can possibly avoid it.  I've been using AWK for
>> (mumble, mumble) years now, and even now, whenever I code an algorithm
>that
>> uses getline, I almost always later determine it to be bad design.

>> In you case:

>> awk '{print;exit}' test.txt

>Am I missing something or shouldn't you just be using 'head -1
>inputfilename'

Not everyone runs on a Unix system where head is available.  :-)

In the original poster's case, I suspect he was just trying to
get a handle on the getline command for future use.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 07 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?


Quote:


>% echo "a" | awk 'BEGIN{getline first<"test.txt"
>%           print first
>%           close("test.txt")}'

>This makes no sense.

You're right.  It doesn't.

Quote:
>An awk program consists of patterns and actions. If all of the patterns
>are BEGIN, then awk doesn't look for an input file:

Here I think you're in trouble.  Classic awk (i.e., Solaris awk) is
defective in this regard.  You need nawk (or better) to get the right
behavior.


Sat, 07 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?

% echo "a" | awk 'BEGIN{getline first<"test.txt"
%               print first
%               close("test.txt")}'

This makes no sense.

An awk program consists of patterns and actions. If all of the patterns
are BEGIN, then awk doesn't look for an input file:
 awk 'BEGIN { getline first < "test.txt"; print first; close("test.txt") }'
(without any input being piped to it) will work fine.

If any of the patterns are not BEGIN, then awk looks at the array ARGV
for the names of input files. If it doesn't find any, it starts reading
from standard input.

--

Patrick TJ McPhee
East York  Canada



Sun, 08 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?


Quote:


>% echo "a" | awk 'BEGIN{getline first<"test.txt"
>%           print first
>%           close("test.txt")}'

>This makes no sense.

>An awk program consists of patterns and actions. If all of the patterns
>are BEGIN, then awk doesn't look for an input file:
> awk 'BEGIN { getline first < "test.txt"; print first; close("test.txt") }'
>(without any input being piped to it) will work fine.

>If any of the patterns are not BEGIN, then awk looks at the array ARGV
>for the names of input files. If it doesn't find any, it starts
>reading from standard input.

I believe I have had problems with awk programs with only BEGIN
sections and no input, and certainly providing input to an awk program
will not cause problems if it is not actually needed or used.

and changing from:

echo "a" | awk 'BEGIN{getline first<"test.txt"
                print first
                close("test.txt")}'

to this:

echo "a" | awk '{getline first<"test.txt"
                 print first
                 close("test.txt")}'

becomes less problematical and offers a certain flexibility for
"playing around" with code or demonstrating how one _MIGHT_ use
getline.

Obviously, if the real object were to read and print the first line
of the file test.txt there are better methods.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 08 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?
Quote:



>% echo "a" | awk 'BEGIN{getline first<"test.txt"
>% print first
>% close("test.txt")}'

>This makes no sense.

Well, not on reasonable awks like gawk, mawk, nawk.  On Solaris awk is oawk
by default and if you have just a BEGIN block it needs _some_ input hence
the echo ... can be useful.

Moral: don't use "awk" unadorned on Solaris! use nawk or /usr/xpg4/awk

Peter
--


Opinions expressed are my own and not necessarily those
of my employer



Sun, 08 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?


Quote:


> >Hi,
> >I am trying to use getline to read first line from the text file:

> >awk '{getline first<"test.txt"}; {print first}; {close("test.txt")}'

> >However, awk still seems  to expect filename from the standard
input.
> >Can someone tell me where is the error here?

> You're not teling awk what to operate on, you're specifying a command
> and not giving it the thing the command is to operate upon.

> Awk expects to deal with _SOME_ input, so give it some, either an
> existing file, or something from standard input like this:

> echo "a" | awk '{getline first<"test.txt"};
>            {print first}; {close("test.txt")}'

> Note that if you used this with a file input, it would output the
> first line of test.txt for each line in your input file.

> Better would be:

> echo "a" | awk 'BEGIN{getline first<"test.txt"
>            print first
>            close("test.txt")}'

> or

> awk 'BEGIN{getline first<"test.txt"
>            print first
>            close("test.txt")}' infile

> Where infile is any multiline file, and this awk code would only
> print the first line of test.txt once.

> I assume you're aware that there are easier ways to read and print
> the first line of a file.  :-)

> Chuck Demas
> Needham, Mass.

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.


Thanks for the input. No, I do not know which "easier ways" to read
file line-by-line in ksh you mentioned. The only alternatives I can
think of are:
- using of "cut" - but that that doesn't work for me on multi- line
files
- writing of whole thing in "C" with using of "fgets".

I would appreciate if you elaborate it.
Thank you again,
Victor L.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 08 Dec 2002 03:00:00 GMT  
 how do i process each line in a text file until it reaches EOF?

Quote:


> % echo "a" | awk 'BEGIN{getline first<"test.txt"
> %          print first
> %          close("test.txt")}'

> This makes no sense.

> An awk program consists of patterns and actions. If all of the patterns
> are BEGIN, then awk doesn't look for an input file:
>  awk 'BEGIN { getline first < "test.txt"; print first; close("test.txt") }'
> (without any input being piped to it) will work fine.

> If any of the patterns are not BEGIN, then awk looks at the array ARGV
> for the names of input files. If it doesn't find any, it starts reading
> from standard input.

What role does the word `first' play in this.  It seem you can do the
same without it or with anyword there.  It still prints the first
line.

awk 'BEGIN { getline  < "test.txt"; print ; close("test.txt") }'

Does the same thing.  Or is it just some kind of convention?



Mon, 09 Dec 2002 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. How to wait until all forked process are done

2. HOW READ A FILE UNTIL EOF ???

3. eof for text file

4. Ascii Text file, eof marker

5. Read a text file line by line

6. how to read text files line by line?

7. eof fails when reading encrypted data in text file

8. I want to stop a while condition when I read a EOF of text file

9. I want to stop a while condition when I read a EOF of text file

10. How do I read lines of Text from Text File and add them to Array

11. eof on channel, not eof on transform: [eof] returns true

12. Text widget --screen lines vs. text lines

 

 
Powered by phpBB® Forum Software