Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
https://forum.ivorde.com/

How to replace \w SED multiple matches on the same line without piping
https://forum.ivorde.com/how-to-replace-w-sed-multiple-matches-on-the-same-line-without-piping-t15511.html
Page 1 of 1

Author:  mandrei99 [ Tue Oct 08, 2013 6:29 am ]
Post subject:  How to replace \w SED multiple matches on the same line without piping

SED's replacing mechanism replaces only the first occurrence each line. To change this, use the "/g" flag.

Example when sed replaces first match only:
Code:
[root@rhel5 ~]# echo "test test asdf test" >test.html
[root@rhel5 ~]# echo "test test asdf test" >>test.html
[root@rhel5 ~]# sed -i 's/test/replacement/' test.html
[root@rhel5 ~]# cat test.html
replacement test asdf test
replacement test asdf test


Example when sed replaces all matches on each line:
Code:
[root@rhel5 ~]# echo "test test asdf test" >test.html
[root@rhel5 ~]# echo "test test asdf test" >>test.html
[root@rhel5 ~]# sed -i 's/test/replacement/g' test.html
[root@rhel5 ~]# cat test.html
replacement replacement asdf replacement
replacement replacement asdf replacement

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/