Set variable in current shell from awk

后端 未结 7 1949
花落未央
花落未央 2020-12-29 05:40

Is there a way to set a variable in my current shell from within awk?

I\'d like to do some processing on a file and print out some data; since I\'ll rea

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 06:09

    You can't export variables from a subshell to its parent shell. You have some other choices, though, including:

    1. Make another pass of the file using AWK to count records, and use command substitution to capture the result. For example:

      FNR=$(awk 'END {print FNR}' filename)
      
    2. Print FNR in the subshell, and parse the output in your other process.
    3. If FNR is the same as number of lines, you can call wc -l < filename to get your count.

提交回复
热议问题