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
我想将user_default下的前缀替换为/ user
I know how I can replace in single line, but I don't know how to check the previous lines.
我知道如何在单行中替换,但我不知道如何检查前面的行。
4 个解决方案
#1
1
This might work for you (GNU sed):
这可能适合你(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.
这会将节标题复制到保留空间中,然后将其附加到该节中的行。如果该行包含user_default:和prefix:它将执行所需的替换。
N.B. It uses the multi-line switch M
to check that the section header begins with the required label.
注:它使用多行开关M来检查节头是否以所需标签开头。
EDIT: Missed the obvious!:
编辑:错过了显而易见的!:
sed -r '/^user_default:/,/^\s*prefix:/{s/\(prefix:\s*).*/\1\/user/}' file
#2
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:
基本上用sed并不是那么容易,但它是可行的(如果sed是唯一的选择,请参阅此答案并根据您的需要进行修改)。我建议使用awk来完成这项工作,例如:
awk '/^user_default:/ { print ; ud=1 ; next}
/^ +resource:/ && ud==1 {print gensub("@UserDefaultBundle","/user",1) ; ud=0 ; next }
{ print }' INPUTFILE
#3
0
This AWK solution should work, change the variables accordingly.
这个AWK解决方案应该工作,相应地更改变量。
awk -v p="prefix:" -v x="user_default:" '{
{!/^[[:space:]]/ && NF=1 && a=$NF}
{if ((a==x) && ($0~p))
sub(/\//,"/user")}
}1' filename
#4
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.
如果user_default会话中不存在“prefix”,则此命令将避免将更改应用于错误的会话。
#1
1
This might work for you (GNU sed):
这可能适合你(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.
这会将节标题复制到保留空间中,然后将其附加到该节中的行。如果该行包含user_default:和prefix:它将执行所需的替换。
N.B. It uses the multi-line switch M
to check that the section header begins with the required label.
注:它使用多行开关M来检查节头是否以所需标签开头。
EDIT: Missed the obvious!:
编辑:错过了显而易见的!:
sed -r '/^user_default:/,/^\s*prefix:/{s/\(prefix:\s*).*/\1\/user/}' file
#2
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:
基本上用sed并不是那么容易,但它是可行的(如果sed是唯一的选择,请参阅此答案并根据您的需要进行修改)。我建议使用awk来完成这项工作,例如:
awk '/^user_default:/ { print ; ud=1 ; next}
/^ +resource:/ && ud==1 {print gensub("@UserDefaultBundle","/user",1) ; ud=0 ; next }
{ print }' INPUTFILE
#3
0
This AWK solution should work, change the variables accordingly.
这个AWK解决方案应该工作,相应地更改变量。
awk -v p="prefix:" -v x="user_default:" '{
{!/^[[:space:]]/ && NF=1 && a=$NF}
{if ((a==x) && ($0~p))
sub(/\//,"/user")}
}1' filename
#4
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.
如果user_default会话中不存在“prefix”,则此命令将避免将更改应用于错误的会话。