使用awk打印文件的一部分 [英] Print parts of file using awk

查看:112
本文介绍了使用awk打印文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于要打印的内容,我有几个条件(要打印的部分中包括跳过问好,从\ k {f}打印到\ l {k},从\ word {g}打印到\ word2 {g},打印以\ hello2开头的行,并打印\ b和\ bf之间的部分-存在问题:在\ bf}中是},不应打印):

I have several conditions for what I want to print (skip hello that is included in part I would like to print, print from \k{f} to \l{k}, from \word{g} to \word2{g}, print row starting \hello2 and print the part between \b and \bf - there is a problem: in \bf} is } that should not to be printed):

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

我想为\ bf添加条件,使其在行中应单独存在.请怎么做?

I would like to add condition for \bf that it should be alone in the row. How to do that please?

file.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 is related to: this question

推荐答案

添加条件以将\ bf}替换为\ bf

Add a condition to replace \bf} with \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

这篇关于使用awk打印文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆