Hi i have problem i cannot rename just only 1st occurence placed on 9th row of string after/since matched pattern
this is input (file containing 30k lines):
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
i tried this, but its renaming on all strings "name"
awk '/This is pattern/ && NR==10 ; sub(/name/,"patternname")1' num
"_https://stackoverflow.com/questions/51678717/print-mth-column-of-nth-line-after-a-match-if-found-in-a-file-using-awk"
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
Something like this should be ok:
awk '/This is pattern/{n=NR};NR==n+9{sub(/name/,"patternname")};1'
Some comments about your try. You wrote
awk '/This is pattern/ && NR==10 ; sub(/name/,"patternname")1' num
awk commands follow the pattern
condition1{action1};condition2{action2};....
If the {action}
part is missing, the default action is {print}
.
If the condition
part is missing, the default condition is 1
(=always true)
As a result , your awk script equals to this:
awk '/This is pattern/ && NR==10{print};sub(/name/,"patternname"){print};1{print}'
Moreover, awk internal variable NR
holds the line number being processed of the input file.
As a result the first part of your script '/This is pattern/ && NR==10{print}
prints the line only when This is pattern
found and NR
(line number) is 10
meaning never in your case.
The second part of your script sub(/name/,"patternname"){print}
, uses the function sub
as a condition to print the line.
So for every line being processed, sub
tries to replace name
with patternname
. if this replacement is sucessfull , the line is then {print}
.
The third part of your script 1{print}
, prints all the other lines, since the condition is 1
(always true).
About my solution:
First part /This is pattern/{n=NR}
, holds in a temp variable n
the line number NR
in which This is pattern
was found.
Second part NR==n+9{sub(/name/,"patternname")}
compares NR
(line number being processed by awk) to n+9
(line number of This is Pattern
+ 9 more lines), and when this condition becomes true, then name
is replaced by patternname
using sub
which is enclosed in {...}
to dictate that this is the action part of the NR==n+9
condition.
Third part 1
, just prints all the other lines (condition 1
==true
, action is missing , default action {print}
is performed)