1

I have this file:

user_default:
    resource: "@UserDefaultBundle/Controller/"
    type:     annotation
    prefix:   /
Other_default:
    resource: "@PeopDefaultBundle/Controller/"
    type:     annotation
    prefix:   /

I want to replace the prefix under user_default to /user

I know how I can replace in single line, but I don't know how to check the previous lines.

mklement0
  • 382,024
  • 64
  • 607
  • 775
user3147180
  • 923
  • 3
  • 12
  • 25

4 Answers4

1

This might work for you (GNU sed):

sed -r '/^\S/{h;b};G;/^user_default:/M{s/(prefix:\s*\S).*/\1user/};P;d' /file

This copies a section header into the hold space and thereafter appends it to lines within that section. If the line contains both user_default: and prefix: it does the required substitution.

N.B. It uses the multi-line switch M to check that the section header begins with the required label.

EDIT: Missed the obvious!:

sed -r '/^user_default:/,/^\s*prefix:/{s/\(prefix:\s*).*/\1\/user/}' file
potong
  • 55,640
  • 6
  • 51
  • 83
0

Basically it's not that easy with sed, but it's doable (see this answer and modify to suit to your needs if sed is the only option). I'd recommend to use awk for this job, like:

awk '/^user_default:/ { print ; ud=1 ; next}
     /^ +resource:/ && ud==1 {print gensub("@UserDefaultBundle","/user",1) ; ud=0 ; next }
     { print }' INPUTFILE
Community
  • 1
  • 1
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
0

This AWK solution should work, change the variables accordingly.

awk -v p="prefix:" -v x="user_default:" '{
{!/^[[:space:]]/ && NF=1 && a=$NF}
{if ((a==x) && ($0~p))
sub(/\//,"/user")}
}1' filename
Ell
  • 927
  • 6
  • 10
0

Using sed

sed -r '/user_default:/{:a;N;/prefix/!{/\n\S/!ba};s!(prefix:\s*).*!\1/user!}' file

This command will avoid to apply the change to wrong session, if "prefix" is not exist in user_default session.

BMW
  • 42,880
  • 12
  • 99
  • 116