aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2024-01-30 21:32:56 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2024-02-07 23:10:55 +0100
commit8fdf124210ae6dfa48ece265a76c14ecde1ca78b (patch)
tree9e838c0a365e7bc53676fbfdb608926fe39451b3
parentf3e631f7539c71686d1e336756c9c0fcd0b587ad (diff)
downloadnotes-8fdf124210ae6dfa48ece265a76c14ecde1ca78b.tar.gz
notes-8fdf124210ae6dfa48ece265a76c14ecde1ca78b.zip
sed: insert pattern + follow symlinks
-rw-r--r--src/tools/sed.md24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/tools/sed.md b/src/tools/sed.md
index 1bd8185..5b5f741 100644
--- a/src/tools/sed.md
+++ b/src/tools/sed.md
@@ -6,6 +6,8 @@ sed [opts] [script] [file]
-i edit file in place
-i.bk edit file in place and create backup file
(with .bk suffix, can be specified differently)
+ --follow-symlinks
+ follow symlinks when editing in place
-e SCRIPT add SCRIPT to commands to be executed
(can be specified multiple times)
-f FILE add content of FILE to command to be executed
@@ -60,6 +62,12 @@ echo -e 'aa\nbb' | sed '2aABC'
echo -e 'aa\nbb' | sed '2cABC'
# aa
# ABC
+
+# Insert before pattern match.
+echo -e 'aa\nbb' | sed '/bb/i 123'
+# aa
+# 123
+# bb
```
### Substitute lines
@@ -76,3 +84,19 @@ echo -e 'foo\nbar' | sed -e 's/foo/FOO/' -e 's/FOO/BAR/'
# BAR
# bar
```
+
+### Edit inplace through symlink
+```sh
+touch file
+ln -s file link
+ls -l link
+# lrwxrwxrwx 1 johannst johannst 4 Feb 7 23:02 link -> file
+
+sed -i --follow-symlinks '1iabc' link
+ls -l link
+# lrwxrwxrwx 1 johannst johannst 4 Feb 7 23:02 link -> file
+
+sed -i '1iabc' link
+ls -l link
+# -rw-r--r-- 1 johannst johannst 0 Feb 7 23:02 link
+```