AWK,仅在模式匹配后的第 n 行重命名字符串
AWK, rename string only on n-th line after pattern match
你好,我有问题,我不能只重命名字符串第 9 行的第 1 次出现 after/since 匹配的模式
这是输入(包含 30k 行的文件):
This is pattern
patternvalue=dom.value.5.row.2
design=12
face=x1-m
omit=11
mode=OFF
option=955
display=x1-11-OFF
type=2
name=8a9s7fa645sdf
resolution=0
prio=OK
number of pattern values:
pattern values
id=hex00.EA
name=4fda6sd4f
number of pattern values:
id=hex00.EF
name=as7e8w87e
patternvalue=dom.value.5.row.8
design=1
face=x1-n
omit=12
mode=OFF
option=95
display=x1-22-ON
type=2
name=8a9sad8f
resolution=0
prio=OK
number of pattern values:
pattern values
id=hex00.0A
name=dsf79
number of pattern values:
id=hex00.AA
name=777777s
number of pattern values:
id=hex00.BB
name=777777l
number of pattern values:
id=hex00.CC
name=777777m
我试过了,但它对所有字符串都进行了重命名 "name"
awk '/This is pattern/ && NR==10 ; sub(/name/,"patternname")1' num
"_"
This is expected output:
This is pattern
patternvalue=dom.value.5.row.2
design=12
face=x1-m
omit=11
mode=OFF
option=955
display=x1-11-OFF
type=2
patternname=8a9s7fa645sdf
resolution=0
prio=OK
number of pattern values:
pattern values
id=hex00.EA
name=4fda6sd4f
number of pattern values:
id=hex00.EF
name=as7e8w87e
This is pattern
patternvalue=dom.value.5.row.8
design=1
face=x1-n
omit=12
mode=OFF
option=95
display=x1-22-ON
type=2
patternname=8a9sad8f
resolution=0
prio=OK
number of pattern values:
pattern values
id=hex00.0A
name=dsf79
number of pattern values:
id=hex00.AA
name=777777s
number of pattern values:
id=hex00.BB
name=777777l
number of pattern values:
id=hex00.CC
name=777777m
Thank you for any hints
像这样应该没问题:
awk '/This is pattern/{n=NR};NR==n+9{sub(/name/,"patternname")};1'
关于您的尝试的一些评论。
你写了
awk '/This is pattern/ && NR==10 ; sub(/name/,"patternname")1' num
awk 命令遵循模式
condition1{action1};condition2{action2};....
如果缺少 {action}
部分,则默认操作为 {print}
。
如果缺少 condition
部分,则默认条件为 1
(=始终为真)
因此,您的 awk 脚本等于:
awk '/This is pattern/ && NR==10{print};sub(/name/,"patternname"){print};1{print}'
此外,awk 内部变量NR
保存输入文件正在处理的行号。
因此,脚本的第一部分 '/This is pattern/ && NR==10{print}
仅在找到 This is pattern
且 NR
(行号)为 10
时才打印该行,这意味着从不在您的案例.
脚本的第二部分 sub(/name/,"patternname"){print}
,使用函数 sub
作为打印该行的条件。
因此,对于正在处理的每一行,sub
尝试将 name
替换为 patternname
。如果此替换成功,则该行为 {print}
.
脚本的第三部分 1{print}
打印所有其他行,因为条件是 1
(始终为真)。
关于我的解决方案:
第一部分 /This is pattern/{n=NR}
,在临时变量 n
中保存找到 This is pattern
的行号 NR
。
第二部分 NR==n+9{sub(/name/,"patternname")}
将 NR
(awk 处理的行号)与 n+9
(This is Pattern
的行号 + 9 行)进行比较,当这条件变为真,然后使用 sub
将 name
替换为 patternname
,sub
包含在 {...}
中,指示这是 NR==n+9
条件的操作部分。
第三部分1
,仅打印所有其他行(条件1
==true
,缺少操作,执行默认操作{print}
)
你好,我有问题,我不能只重命名字符串第 9 行的第 1 次出现 after/since 匹配的模式
这是输入(包含 30k 行的文件):
This is pattern
patternvalue=dom.value.5.row.2
design=12
face=x1-m
omit=11
mode=OFF
option=955
display=x1-11-OFF
type=2
name=8a9s7fa645sdf
resolution=0
prio=OK
number of pattern values:
pattern values
id=hex00.EA
name=4fda6sd4f
number of pattern values:
id=hex00.EF
name=as7e8w87e
patternvalue=dom.value.5.row.8
design=1
face=x1-n
omit=12
mode=OFF
option=95
display=x1-22-ON
type=2
name=8a9sad8f
resolution=0
prio=OK
number of pattern values:
pattern values
id=hex00.0A
name=dsf79
number of pattern values:
id=hex00.AA
name=777777s
number of pattern values:
id=hex00.BB
name=777777l
number of pattern values:
id=hex00.CC
name=777777m
我试过了,但它对所有字符串都进行了重命名 "name"
awk '/This is pattern/ && NR==10 ; sub(/name/,"patternname")1' num
"_
This is expected output:
This is pattern
patternvalue=dom.value.5.row.2
design=12
face=x1-m
omit=11
mode=OFF
option=955
display=x1-11-OFF
type=2
patternname=8a9s7fa645sdf
resolution=0
prio=OK
number of pattern values:
pattern values
id=hex00.EA
name=4fda6sd4f
number of pattern values:
id=hex00.EF
name=as7e8w87e
This is pattern
patternvalue=dom.value.5.row.8
design=1
face=x1-n
omit=12
mode=OFF
option=95
display=x1-22-ON
type=2
patternname=8a9sad8f
resolution=0
prio=OK
number of pattern values:
pattern values
id=hex00.0A
name=dsf79
number of pattern values:
id=hex00.AA
name=777777s
number of pattern values:
id=hex00.BB
name=777777l
number of pattern values:
id=hex00.CC
name=777777m
Thank you for any hints
像这样应该没问题:
awk '/This is pattern/{n=NR};NR==n+9{sub(/name/,"patternname")};1'
关于您的尝试的一些评论。 你写了
awk '/This is pattern/ && NR==10 ; sub(/name/,"patternname")1' num
awk 命令遵循模式
condition1{action1};condition2{action2};....
如果缺少 {action}
部分,则默认操作为 {print}
。
如果缺少 condition
部分,则默认条件为 1
(=始终为真)
因此,您的 awk 脚本等于:
awk '/This is pattern/ && NR==10{print};sub(/name/,"patternname"){print};1{print}'
此外,awk 内部变量NR
保存输入文件正在处理的行号。
因此,脚本的第一部分 '/This is pattern/ && NR==10{print}
仅在找到 This is pattern
且 NR
(行号)为 10
时才打印该行,这意味着从不在您的案例.
脚本的第二部分 sub(/name/,"patternname"){print}
,使用函数 sub
作为打印该行的条件。
因此,对于正在处理的每一行,sub
尝试将 name
替换为 patternname
。如果此替换成功,则该行为 {print}
.
脚本的第三部分 1{print}
打印所有其他行,因为条件是 1
(始终为真)。
关于我的解决方案:
第一部分 /This is pattern/{n=NR}
,在临时变量 n
中保存找到 This is pattern
的行号 NR
。
第二部分 NR==n+9{sub(/name/,"patternname")}
将 NR
(awk 处理的行号)与 n+9
(This is Pattern
的行号 + 9 行)进行比较,当这条件变为真,然后使用 sub
将 name
替换为 patternname
,sub
包含在 {...}
中,指示这是 NR==n+9
条件的操作部分。
第三部分1
,仅打印所有其他行(条件1
==true
,缺少操作,执行默认操作{print}
)