From 7328774d81bf8feef6d6ff00f8cbb6ba8300c46e Mon Sep 17 00:00:00 2001 From: Leandro Ostera Date: Sat, 27 Feb 2016 14:23:24 +0100 Subject: [PATCH 1/2] Sed: adds replace before matching sample Something I find myself doing more and more is to match on a line instead of trying to match all I want with `{{find}}`. So in a file like: ```erl { riak_core, [ { {riak_ip, "127.0.0.1"}, %% ... }, %% ... ] ``` Instead of replacing that ip by doing `sed 's/riak_ip,"127\.0\.0\.1/riak_ip,"$NEW_IP/'`, I do `sed '/riak_ip/s/127\.0\.0\.1/$NEW_IP'`. --- pages/common/sed.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pages/common/sed.md b/pages/common/sed.md index b4054c9a7..1e538a22a 100644 --- a/pages/common/sed.md +++ b/pages/common/sed.md @@ -6,6 +6,10 @@ `sed 's/{{find}}/{{replace}}/' {{filename}}` +- Replace the first occurrence on all lines matching a pattern: + +`sed '/{{line_pattern}}/s/{{find}}/{{replace}}/'` + - Replace all occurrences of a string in a file, overwriting the file (i.e. in-place): `sed -i 's/{{find}}/{{replace}}/g' {{filename}}` From e22f8087e10a77ecddb52de4cde50b8ac37a114d Mon Sep 17 00:00:00 2001 From: Leandro Ostera Date: Tue, 1 Mar 2016 12:54:15 +0100 Subject: [PATCH 2/2] Updates description --- pages/common/sed.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/common/sed.md b/pages/common/sed.md index 1e538a22a..51b1dcc22 100644 --- a/pages/common/sed.md +++ b/pages/common/sed.md @@ -6,7 +6,7 @@ `sed 's/{{find}}/{{replace}}/' {{filename}}` -- Replace the first occurrence on all lines matching a pattern: +- Replace only on lines matching the line pattern: `sed '/{{line_pattern}}/s/{{find}}/{{replace}}/'`