0

I'm trying to use awk to replace a multiple string in a file. I have a repeated set of lines that I need to replace and delete. For example the file has

INSTANCE "INVX1":"physical"
"A" : "reset"
"Y" : "pp_resetbar"

INSTANCE "INVX1":"physical"
"A" : "reset"
"Y" : "pp_resetbar"

INSTANCE "INVX1":"physical"
"A" : "reset"
"Y" : "pp_resetbar"

and I want to change/replace the first two and delete the 3rd, 4th, ..., Nth

INSTANCE "INVX1":"physical"
"A" : "reset"
"Y" : "pp_resetbar_b"

INSTANCE "BUFX2":"physical"
"A" : "pp_resetbar_b"
"Y" : "pp_resetbar"

To be honest I don't even know where to start. I created a script to replace the Nth occurrence but doesn't seem work for this. See below. Any help will be more than appreciated.

awk -v search=$2 -v replace=$3 -v cnt=$4 '$0 ~ search{c++;if(c==cnt){sub(search,replace);}}1' "$file" > temp && mv temp "$file"
4

1 回答 1

1

未经测试但应该接近:

awk -v RS= -v ORS="\n\n" '
    { c = ++count[$0] }
    c == 1 { <replace some stuff> }
    c == 2 { <replace other stuff> }
    c <= 2
' file
于 2012-10-29T02:54:26.373 回答