Commands using variables

  • Follow


I am trying to count the number of lines in a source file (let's call it "src"). This seems to work:

BEGIN {
"cat src | wc -l" | getline l
print l
}

But this doesn't work:

BEGIN {
variable = src
"cat " variable " | wc -l" | getline l
print l
}

Basically I want to use a variable in place of the filename since my script will go through a bunch of source files.

Am I doing something wrong? (I am quite the novice at this.)
0
Reply celiborn 2/24/2011 3:40:48 AM

On 2/23/2011 9:40 PM, celiborn wrote:
> I am trying to count the number of lines in a source file (let's call it "src"). This seems to work:
>
> BEGIN {
> "cat src | wc -l" | getline l
> print l
> }
>
> But this doesn't work:
>
> BEGIN {
> variable = src

The above assigns variable variable the value of the variable src. If you want 
to assign variable variable to be the string "src" then you'd do:

variable = "src"

> "cat " variable " | wc -l" | getline l
> print l
> }
>
> Basically I want to use a variable in place of the filename since my script will go through a bunch of source files.
>
> Am I doing something wrong? (I am quite the novice at this.)

You're trying to use awk as a shell interpreter. That's always completely the 
wrong approach since the shell is a perfectly good interpreter without awk 
getting in the way. Also, don't use getline until you fully appreciate 
everything that's described at http://awk.info/?tip/getline.

If you really want to use awk to count the lines in a file, then you'd just do:

awk 'END{ print NR }' file

If you want to do it for multiple files then it's:

awk '
    FNR==1 && prev { print prev }
    { prev=FILENAME ":" FNR }
    END { print prev }
' file1 file2 ...

Regards,

	Ed.
0
Reply Ed 2/24/2011 4:32:21 AM


El 24/02/2011 5:32, Ed Morton escribi�:
>[...]
> If you really want to use awk to count the lines in a file, then you'd
> just do:
>
> awk 'END{ print NR }' file
>
> If you want to do it for multiple files then it's:
>
> awk '
> FNR==1 && prev { print prev }
> { prev=FILENAME ":" FNR }
> END { print prev }
> ' file1 file2 ...

The second approach is a typical use case for the ENDFILE extension 
available in the development version of gawk:

gawk4 'ENDFILE { print FILENAME ": " FNR }' file1 file2 ...

Besides the simplicity of the code, an advantage of this extension is 
that zero-length files are also reported.

Regards,
-- 
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado

0
Reply Manuel 2/24/2011 10:58:42 AM

On 2/24/2011 4:58 AM, Manuel Collado wrote:
> El 24/02/2011 5:32, Ed Morton escribi�:
>> [...]
>> If you really want to use awk to count the lines in a file, then you'd
>> just do:
>>
>> awk 'END{ print NR }' file
>>
>> If you want to do it for multiple files then it's:
>>
>> awk '
>> FNR==1 && prev { print prev }
>> { prev=FILENAME ":" FNR }
>> END { print prev }
>> ' file1 file2 ...
>
> The second approach is a typical use case for the ENDFILE extension available in
> the development version of gawk:
>
> gawk4 'ENDFILE { print FILENAME ": " FNR }' file1 file2 ...
>
> Besides the simplicity of the code, an advantage of this extension is that
> zero-length files are also reported.
>
> Regards,

True. To accommodate zero-length files otherwise you'd do something like:

awk '
    { fnr[FILENAME] = FNR }
    END { for (i=1;i<=ARGC; i++) printf "%s:%d\n", ARGV[i], fnr[ARGV[i]] }
' file1 file2 ...

     Ed
0
Reply Ed 2/24/2011 12:48:16 PM

3 Replies
167 Views

(page loaded in 0.049 seconds)

Similiar Articles:













7/30/2012 9:30:58 AM


Reply: