【问题标题】:Print parts of file using awk使用 awk 打印部分文件
【发布时间】:2019-11-03 02:04:01
【问题描述】:

我想要打印的内容有几个条件(跳过包含在我想要打印的部分中的 hello,从 \k{f} 打印到 \l{k},从 \word{g} 到 \word2 {g},打印从 \hello2 开始的行并打印 \b 和 \bf 之间的部分 - 有一个问题:在 \bf} 中是不应该打印的}):

    awk '
/\\hello/{
  next
}
/\\k\{f\}|\\word\{g\}|\\b/{
  found=1
}
found;
/\\l\{f\}|\\word2\{g\}|\\bf/{
  found=""
}
/\\hello2/
' file.txt

我想为 \bf 添加条件,它应该在行中单独存在。请问该怎么做?

文件.txt:

text
text
\hello2
456
565
\word{g}
s
\hello
\word2{g}
\k{f}
fdsfd
fgs
\l{f}
text
\b
7
\hello
\bf}
text

现在输出:

\word{g}
s
\word2{g}
\k{f}
fdsfd
fgs
\l{f}
\b
7
\bf}

想要的输出:

\word{g}
s
\word2{g}
\k{f}
fdsfd
fgs
\l{f}
\b
7
\bf

此问题与:this question

【问题讨论】:

  • 能否在您的问题中更清楚地添加条件,例如-->I want to print from \k{f} to l{f}` 等?目前还不清楚。
  • 我在我的问题中添加了它
  • 请不要发布变色龙问题,请参阅meta.stackexchange.com/q/43478/361691

标签: awk


【解决方案1】:

添加一个条件,用\bf替换\bf}

    awk '
/\\hello/{
  next
}
/\\k\{f\}|\\word\{g\}|\\b/{
  found=1
}
# Fix BF lines
/\\bf}/ { $0 = "\\bf" }
# 

found;
/\\l\{f\}|\\word2\{g\}|\\bf/{
  found=""
}
/\\hello2/
' file.txt

【讨论】:

  • 非常感谢您能帮我解决这个脚本的另一个条件吗?我有行text text text \b text text 45 \bf 文本如何仅打印\b text text 45 \bf
  • 有多种可能的输入会失败,例如一些见*.com/questions/58671993/…。 @Archie - 接受您得到的第一个答案是假设这是您可以获得的最佳答案,因为看到已经接受的答案会阻止其他人发布替代方案。也写could you help me with one more condition - 看看我给你的关于你之前的问题的建议:*.com/questions/58671993/…
最近更新 更多