【发布时间】:2020-06-03 10:49:22
【问题描述】:
我似乎无法获得动态交换 FS/RS 变量的技巧,因此我从输入中得到以下结果:
输入文件
header 1
header 2
{
something should not be removed
}
50
(
auto1
{
type good;
remove not useful;
}
auto2
{
type good;
keep useful;
}
auto3
{
type moderate;
remove not useful;
}
)
输出文件
header 1
header 2
{
something that should not be removed
}
50
(
auto1//good
{
type good;//good
}
auto2//good
{
type good;//good
keep useful;
}
auto3//moderate
{
type moderate;//moderate
}
)
关键是:
- 如果代码块
{...}前面没有autoX(X 可以是 1、2、3 等),则不会发生任何变化。 - 当
autoX后跟代码块{...}时,应该会发生更改。 - 代码块&
autoX内的值被修改为添加\\good或//moderate,需要从{...}本身读取。 - 如果包含短语
remove,则应从{...}中删除整行。
提示:这可能是可以使用regex 和here 解释的想法,特别是example。
目前,我只能满足最后一个要求,代码如下:
awk ' {$1=="{"; FS=="}";} {$1!="}"; gsub("remove",""); print NR"\t\t"$0}' Input_file
提前感谢您的技能和时间,通过awk 解决此问题。
【问题讨论】:
标签: regex awk regex awk environment-variables text-manipulation