Passing bash variable as a pattern to awk

前端 未结 1 855
旧时难觅i
旧时难觅i 2021-01-23 05:30

I would like to know how to pass a bash variable as a pattern to awk. I have read several Q/As which tend to answer the same question, how

1条回答
  •  攒了一身酷
    2021-01-23 05:57

    Variables aren't expanded in regexp literals (/.../). You need to use the ~ operator and string literals.

    awk -v pattern="$y" '$0 ~ "^"pattern {print $2}' "$x" > "freec_${x}_${y}_st.txt"
    

    You also need to be careful with any pattern metacharacters/etc. in the pattern.

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