2

Expanding on Print the last line of a file, from the CLI

I'm calling awk and passing * to process all files in the folder, but can't seem to figure out how to dump my variables after each file.

  • The END keyword seems to run only after all files have completed:
    • awk -F"'" '/export const/ { print $0 } END { print "done" }' *
    • awk -F"'" '/export const/ { CLSNM = $0 } END {print CLSNM }' *
  • And these don't fire at all (no output):
    • awk -F"'" '/export const/ { CLSNM = $0 } NR==filesize { print CLSNM }' *
    • awk -F"'" '/export const/ { CLSNM = $0 } NR==LNR { print CLSNM }' *

I'm on MacOS using GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)

0

1 Answer 1

4

Action you are looking to be done should be covered with ENDFILE, try following code once. This is a GNU awk option, should work for your shown versioned of awk.

awk -F"'" '/export const/ { CLSNM = $0 } ENDFILE{ print CLSNM;CLSNM="" }' *

2nd solution: without EBNDFILE option try following, should work with any awk version.

awk -F"'" 'FNR==1 && CLSNM{ print CLSNM; CLSNM=""} /export const/ { CLSNM = $0 } END{if(CLSNM){print CLSNM}}' *
Sign up to request clarification or add additional context in comments.

1 Comment

conversely, BEGINFILE also turned out to be useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.