|
|
Commands using variables
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: Assigning Variables in a parfor loop - comp.soft-sys.matlab ...> > > >But I wanted to know, what if i have say some 100 variables in one reference model and 100 in another... do i have to assign each variable using this assignin command...?? Redirect command output to a variable - comp.unix.programmer ...Hi all, Is it possible to redirect the output of a command directly to a variable without using any intermediate file. For example how to do the f... Capture windows command return code to variable in awk script ...I tried using the system command, but apparently the value that is stored is whether or ... Local array variables in functions - comp.lang.awk ... my DISPLAY variable using ... Finding environment variables usage - comp.unix.programmer ...Hi everybody, How can I find which environment variables a certain process is using? ... Capture windows command return code to variable in awk script ... Finding ... Variable syntax problem for SunOS 5.8 using C Shell - comp.unix ...The "command exit status" in csh is returned in the variable ... Variable syntax problem for SunOS 5.8 using C Shell - comp.unix ... Variable syntax problem for SunOS 5 ... mailx and variables? - comp.unix.solarisIn this case, quotes are needed on the mailx command line. You shouldn't try to ... subj" "$emailto" < $filename So in the lines where you assign values to variables ... Getting the outcome of a system command into SAS macro variable ...I know that in Unix I can use FILENAME PIPE command then use DATA STEP to read in results of a system command. For example, filename abc PIPE "l... migrating mainframe z/VM Rexx to linux ooRexx - comp.lang.rexx ...As for EXECIO, and without knowing your problems, if it was coded with file descriptions on the EXECIO commands instead of using variables, I could see it causing every ... /bin/sh: Local variables in a function ? - comp.unix.solaris ...You can fix this to some extent by using funny names ... comp.unix.solaris ..... the typeset special command used within a function defines local variables whose ... Insert A Variable Using Syntax - comp.soft-sys.stat.spss ...Then re-order the variables using the /KEEP subcommand of either MATCH FILES or ADD FILES. ... very old version of SPSS, or maybe it was SPSS/PC+, had a MODIFY VARS command ... Using Variables and ConstantsEven if you are not required to declare a variable before using it, it is a ... The scope, or lifetime, of a variable determines which script commands can access a variable. The Set command and how to use variables in the Windows command lineHow to use the Set command and variables in the Windows command line. 7/30/2012 9:30:58 AM
|
|
|
|
|
|
|
|
|