【问题标题】:Awk string and add textawk 字符串并添加文本
【发布时间】:2012-10-10 20:01:42
【问题描述】:

我在下面有一个 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?

【问题讨论】:

  • 考虑编辑您的问题以包含示例输入数据和所需的输出。很难猜出你打算在这里完成什么。根据我的解释,这可以通过 1 个 awk 程序来完成。祝你好运。

标签: awk


【解决方案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 one-liner:

sed -e 's/^/Text prepended\n/; s/$/\nText appended/' infile

【讨论】:

  • 谢谢,更多的是寻找单行命令而不是脚本。
  • @Tony:用单行脚本更新。
  • 这不是 awk 解决方案。
猜你喜欢
  • 2013-04-19
  • 1970-01-01
  • 2017-02-18
  • 2017-10-20
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
相关资源
最近更新 更多