Substituting shell variables into awk pattern looking scanning through file

后端 未结 1 1051
刺人心
刺人心 2021-01-25 18:57

NOTE: I am a noob at bash scripts and the awk command - please excuse any dumb mistakes I make.

I am unable to substitute shell variables into my awk pa

1条回答
  •  别那么骄傲
    2021-01-25 19:40

    Could you please try following, haven't tested it because no clear samples but should work.

    funcName="<${2}>:"
    awk_result=$(awk -v FN="$funcName" 'index($0,FN){found=1} found; /^$/{found=""}' "$ofile")
    rfile=search.txt
    echo -e "$awk_result" > "$rfile"
    

    Things fixed in OP's attempt:

    1. NEVER keep same name of a variable as a binary's name or on a keyword's name so changed awk variable name to awk_result.
    2. Use of backticks is depreciated now, so always wrap your variable for having values in var=$(......your commands....) fixed it for awk_result variable.
    3. Now let us talk about awk code fix, I have used index method which checks if value of variable FN is present in a line then make a FLAG(a variable TRUE) and make it false till line is empty as per OP's ask.

    0 讨论(0)
提交回复
热议问题