今天在单位搞个脚本想把shell脚本的一个变量插入到awk里面用,杂搞都不行!!回来试了下,找到个办法,虽然还是比较麻烦。
#! /bin/bash
s=hello world
awk '{print "'$s'"}'
上面那样的拼接可以在awk里面输出外部的shell变量。
有比如有文件file1如下:
this
is
test
用awk生成file2, 如果在file1里面的行有"is", 那么在file2里面就输出“found”,否则还是按file1中的内容输出:
#! /bin/bash
target=found
awk '{if(/is/){print "'$target'"}else{print $0}' file1 > file2