开发者

sed/awk replace word in specific position of sentence

开发者 https://www.devze.com 2023-03-01 05:28 出处:网络
I\'m trying to replace a开发者_JAVA技巧 word at a specific position of a line For example, given:

I'm trying to replace a开发者_JAVA技巧 word at a specific position of a line

For example, given:

INSERT INTO address (city_id,city) VALUES (1,'Monrovia');
INSERT INTO address (city_id,city) VALUES (2,'Brunswick');
INSERT INTO address (city_id,city) VALUES (3,'Phenix City');

I want to replace all different the cities with 'Detroit', resulting in:

INSERT INTO address (city_id,city) VALUES (1,'Detroit');
INSERT INTO address (city_id,city) VALUES (2,'Detroit');
INSERT INTO address (city_id,city) VALUES (3,'Detroit');

Please, How can i do that in sed, awk, or vim?

Thank you.


Huh? That's easy:

$ echo "INSERT INTO address (city_id,city) VALUES (1,'Monrovia');
> INSERT INTO address (city_id,city) VALUES (2,'Brunswick'); 
> INSERT INTO address (city_id,city) VALUES (3,'Phenix City');" | 
> sed "s/'[^']*'/'Detroit'/g"
INSERT INTO address (city_id,city) VALUES (1,'Detroit'); 
INSERT INTO address (city_id,city) VALUES (2,'Detroit'); 
INSERT INTO address (city_id,city) VALUES (3,'Detroit');

Cheers. Keith.


Easiest: Keep the script, append

UPDATE address SET city ='Detroit'

In vim:

:g/^INSERT INTO address/normal f;F'ci'Detroit

In sed:

sed "s/\(VALUES (.*,\)'.*\?'/\1'Detroit'/g"
0

精彩评论

暂无评论...
验证码 换一张
取 消