我在下面有一个awk声明,我想在每个输出行之前和之后添加一些文本。
grep "id\": \"http://mysite.com/movies/new/id/" new_id.txt | head -n1217 | awk -F/ ' { print $7 } ' | awk -F\" ' { print $1 } '
这让我得到了相关ID的列表,但是我需要将每行输出并添加并添加一些文本。我怎么能用awk做到这一点? SED?
答案 0 :(得分:1)
使用GNU sed
预先添加和附加行的一种方法:
假设infile
包含数据:
one
two
three
关注script.sed
:
1,$ {
i\
Text prepended (line 1)\
Text prepended (line 2
a\
Text appended (line 1)\
Text appended (line 2)
}
像以下一样运行:
sed -f script.sed infile
产量:
Text prepended (line 1)
Text prepended (line 2
one
Text appended (line 1)
Text appended (line 2)
Text prepended (line 1)
Text prepended (line 2
two
Text appended (line 1)
Text appended (line 2)
Text prepended (line 1)
Text prepended (line 2
three
Text appended (line 1)
Text appended (line 2)
因此,您需要根据需要进行调整,并将其添加到管道末端。
编辑:sed
单行:
sed -e 's/^/Text prepended\n/; s/$/\nText appended/' infile