I am using below gsutil command to download the list of empty files. I would like to write header as "filename" before output gets written.

gsutil ls -l "gs://test-data-prod-ingest/cm_data/AN/CM/*/a01_*_20210128*.csv.bz2" | awk '{if ($1 <= 100) print $3}' > ls_output.csv

output should be like that


1 Answer 1



...| awk '!hdr{ print "something as header"; hdr=1; }; $1 <= 100{ print $3; }'

this has an advantage of to having printing header line only if there is something read by awk from its stdin, while if we write a header line within BEGIN{...} block, that will print the header line even when no data is read from the input.

or even better to print header only when at least there is one record with the matched condition to print:

...| awk '$1<=100 { if (!hdr) { print "something as header"; hdr=1; }; print $3; } '
  • 1
    Could also use the condition NR == 1 with the same effect. Feb 1, 2021 at 19:32
  • @glennjackman ah, yes, that is also another option. Feb 1, 2021 at 19:35
  • Perfect ! Will it print no header in case there is nothing to write? Feb 1, 2021 at 19:38
  • Could you please also suggest to fix the issue when file name is having spaces in between.. how to search in that case.. for example file name is "510154_Call Dropping Measurement_04Feb2021_1100-04Feb2021_1200.csv.bz2" Feb 17, 2021 at 17:01
  • 1
    hmm, that's the question about gsutil ls -l then which I don't know how it works except that I could only say quoting may resolve the issue while it didn't. I suggest you to open a new question for that. Feb 17, 2021 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .