awk: Using getline to pipe the output of a command into an awk variable doesn't see the output - Stack Overflow

admin2025-04-20  0

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

Share Improve this question edited Mar 3 at 17:31 Scott Petrack asked Mar 3 at 17:27 Scott PetrackScott Petrack 2512 silver badges8 bronze badges 6
  • 1 ITYM print ls_out instead of print $ls_out. – Ed Morton Commented Mar 3 at 17:31
  • Well, that was embarrassing. Thank you. Sometimes, one's brain just has a manual transmission and is stuck in neutral. (And while on the subject (of my non-functional brain atm) -- is there a better way to get the size of a file into an awk variable?) – Scott Petrack Commented Mar 3 at 17:33
  • 1 You're welcome. Also, make sure to read awk.freeshell./AllAboutGetline (if that site is down see the archive at web.archive./web/20221109201352/http://awk.freeshell./…) if you're ever considering using getline. You should also read mywiki.wooledge./ParsingLs since you're apparently considering parsing the output of ls. – Ed Morton Commented Mar 3 at 17:33
  • You are correct, of course, but tbh, that was sort of my problem... I found myself reading all sorts of esoteric and important things about getline. Too bad the problem was with print :) – Scott Petrack Commented Mar 3 at 17:36
  • 3 if all you want is the 'size' (eg, # of bytes) of a file there are better ways than parsing the output from ls; both wc and stat can provide number of bytes with no need for further parsing – markp-fuso Commented Mar 3 at 17:39
 |  Show 1 more comment

1 Answer 1

Reset to default 0

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)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745080960a283853.html

最新回复(0)