My actual problem is that the input to an awk program is a file where some field in every line contains a filename, and I need to put the filesize of the file with that name into an awk variable.
Here is the shortest example of what I clearly do not understand:
[scott@scott-xps9500 extracted]$ cat test.awk
{ "ls -l myfile" | getline ls_out ; print $ls_out }
[scott@scott-xps9500 extracted]$ cat myfile
this is the file myfile
with two lines in it
[scott@scott-xps9500 extracted]$ cat kkk
this is the file kkk
with three lines in it
here is the last line
[scott@scott-xps9500 extracted]$ awk -f test.awk myfile
this is the file myfile
with two lines in it
[scott@scott-xps9500 extracted]$ awk -f test.awk kkk
this is the file kkk
with three lines in it
here is the last line
I thought that the awk program would assign the output of the ls -l myfile
command into the awk variable ls_out. But it just reads a line of input into that variable. That's the behavior if there is no output of the command. But there is!
[scott@scott-xps9500 extracted]$ ls -l myfile
-rw-r--r-- 1 scott scott 45 Mar 3 19:15 myfile
What am I misunderstanding and doing wrong? What I'd like is a test.awk file that no matter what input file i give it, the output will be the same; that is, my fantasy is to see this:
[scott@scott-xps9500 extracted]$ awk -f test.awk AnyOldFileThatExists
-rw-r--r-- 1 scott scott 45 Mar 3 19:15 myfile
Will have some other problem if the name of the file I actually need to use isn't "myfile" but is a string I put together with other awk variables?
Thank you
My actual problem is that the input to an awk program is a file where some field in every line contains a filename, and I need to put the filesize of the file with that name into an awk variable.
Here is the shortest example of what I clearly do not understand:
[scott@scott-xps9500 extracted]$ cat test.awk
{ "ls -l myfile" | getline ls_out ; print $ls_out }
[scott@scott-xps9500 extracted]$ cat myfile
this is the file myfile
with two lines in it
[scott@scott-xps9500 extracted]$ cat kkk
this is the file kkk
with three lines in it
here is the last line
[scott@scott-xps9500 extracted]$ awk -f test.awk myfile
this is the file myfile
with two lines in it
[scott@scott-xps9500 extracted]$ awk -f test.awk kkk
this is the file kkk
with three lines in it
here is the last line
I thought that the awk program would assign the output of the ls -l myfile
command into the awk variable ls_out. But it just reads a line of input into that variable. That's the behavior if there is no output of the command. But there is!
[scott@scott-xps9500 extracted]$ ls -l myfile
-rw-r--r-- 1 scott scott 45 Mar 3 19:15 myfile
What am I misunderstanding and doing wrong? What I'd like is a test.awk file that no matter what input file i give it, the output will be the same; that is, my fantasy is to see this:
[scott@scott-xps9500 extracted]$ awk -f test.awk AnyOldFileThatExists
-rw-r--r-- 1 scott scott 45 Mar 3 19:15 myfile
Will have some other problem if the name of the file I actually need to use isn't "myfile" but is a string I put together with other awk variables?
Thank you
need to put the filesize of the file with that name into an awk variable
GNU AWK sports filefuncs
extension, which allow you to harvest data about file including its' size, for example
awk -v filetoscry="file.txt" '@load "filefuncs";BEGIN{stat(filetoscry,arr);print arr["size"]}'
will output size of selected file (file.txt
in example above). @load
does summon filefuncs
extension, stat
does fill array arr
with information about filetoscry, then I print one of them. Consult User's Guide for other collected data.
(tested in GNU Awk 5.3.1)
print ls_out
instead ofprint $ls_out
. – Ed Morton Commented Mar 3 at 17:31getline
. You should also read mywiki.wooledge./ParsingLs since you're apparently considering parsing the output ofls
. – Ed Morton Commented Mar 3 at 17:33print
:) – Scott Petrack Commented Mar 3 at 17:36ls
; bothwc
andstat
can provide number of bytes with no need for further parsing – markp-fuso Commented Mar 3 at 17:39