Skip to main content
edited body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 ' /two/ {x[NR + offset]} NR in x {delete x[NR]; $0 = "replaced""MODIFIED"} {print}' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 ' /two/ {x[NR + offset]} NR in x {delete x[NR]; $0 = "replaced"} {print}' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 ' /two/ {x[NR + offset]} NR in x {delete x[NR]; $0 = "MODIFIED"} {print}' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

added 27 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 '/two/ {x[NR+offset]}; NR in x {delete x[NR]; $0 = "replaced"} 1' 
awk -v offset=60 ' /two/ {x[NR + offset]} NR in x {delete x[NR]; $0 = "replaced"} {print}' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 '/two/ {x[NR+offset]}; NR in x {delete x[NR]; $0 = "replaced"} 1' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 ' /two/ {x[NR + offset]} NR in x {delete x[NR]; $0 = "replaced"} {print}' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

added 2 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 '/two/ {x[NR+offset]}; NR in x {delete x[NR]; $0 = "replaced"}' 1' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 '/two/ {x[NR+offset]}; NR in x {delete x[NR]; $0 = "replaced"}' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

For this kind of tasks, you may have to consider what to do if the search pattern occurs again before the offset.

awk -v offset=60 '/two/ {x[NR+offset]}; NR in x {delete x[NR]; $0 = "replaced"} 1' 

Would make sure a line is replaced whenever the line 60 rows above contained two.

added 4 characters in body
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading