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"
精彩评论