Print parts of file using awk

2020-05-06 12:50发布

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

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

Output now:

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

The desired output:

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

This question is related to: this question

标签: awk
1条回答
虎瘦雄心在
2楼-- · 2020-05-06 13:17

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
查看更多
登录 后发表回答