From eefe5a6c52623f9c2748e15914bcc5b636b1a4d6 Mon Sep 17 00:00:00 2001 From: Emily Grace Seville Date: Sat, 12 Nov 2022 23:05:16 +1000 Subject: [PATCH] sed: refresh/add pages (#7931) --- pages/common/sed.md | 33 +++++++++++---------------------- pages/osx/sed.md | 36 ------------------------------------ 2 files changed, 11 insertions(+), 58 deletions(-) delete mode 100644 pages/osx/sed.md diff --git a/pages/common/sed.md b/pages/common/sed.md index 33cb8013c..30b1557e3 100644 --- a/pages/common/sed.md +++ b/pages/common/sed.md @@ -1,36 +1,25 @@ # sed > Edit text in a scriptable manner. +> See also: `awk`, `ed`. > More information: . -- Replace the first occurrence of a regular expression in each line of a file, and print the result: +- Replace all `apple` (basic regex) occurrences with `mango` (basic regex) in all input lines and print the result to `stdout`: -`sed 's/{{regular_expression}}/{{replace}}/' {{filename}}` +`{{command}} | sed 's/apple/mango/g'` -- Replace all occurrences of an extended regular expression in a file, and print the result: +- Execute a specific script [f]ile and print the result to `stdout`: -`sed -r 's/{{regular_expression}}/{{replace}}/g' {{filename}}` +`{{command}} | sed -f {{path/to/script.sed}}` -- Replace all occurrences of a string in a file, overwriting the file (i.e. in-place): +- Replace all `apple` (extended regex) occurrences with `APPLE` (extended regex) in all input lines and print the result to `stdout`: -`sed -i 's/{{find}}/{{replace}}/g' {{filename}}` +`{{command}} | sed -E 's/(apple)/\U\1/g'` -- Replace only on lines matching the line pattern: +- Print just a first line to `stdout`: -`sed '/{{line_pattern}}/s/{{find}}/{{replace}}/' {{filename}}` +`{{command}} | sed -n '1p'` -- Delete lines matching the line pattern: +- Replace all `apple` (basic regex) occurrences with `mango` (basic regex) in all input lines and save modifications to a specific file: -`sed '/{{line_pattern}}/d' {{filename}}` - -- Print the first 11 lines of a file: - -`sed 11q {{filename}}` - -- Apply multiple find-replace expressions to a file: - -`sed -e 's/{{find}}/{{replace}}/' -e 's/{{find}}/{{replace}}/' {{filename}}` - -- Replace separator `/` by any other character not used in the find or replace patterns, e.g. `#`: - -`sed 's#{{find}}#{{replace}}#' {{filename}}` +`sed -i 's/apple/mango/g' {{path/to/file}}` diff --git a/pages/osx/sed.md b/pages/osx/sed.md deleted file mode 100644 index e56528871..000000000 --- a/pages/osx/sed.md +++ /dev/null @@ -1,36 +0,0 @@ -# sed - -> Edit text in a scriptable manner. -> More information: . - -- Replace the first occurrence of a string in a file, and print the result: - -`sed 's/{{find}}/{{replace}}/' {{filename}}` - -- Replace all occurrences of an extended regular expression in a file: - -`sed -E 's/{{regular_expression}}/{{replace}}/g' {{filename}}` - -- Replace all occurrences of a string [i]n a file, overwriting the file (i.e. in-place): - -`sed -i '' 's/{{find}}/{{replace}}/g' {{filename}}` - -- Replace only on lines matching the line pattern: - -`sed '/{{line_pattern}}/s/{{find}}/{{replace}}/' {{filename}}` - -- Print only text between n-th line till the next empty line: - -`sed -n '{{line_number}},/^$/p' {{filename}}` - -- Apply multiple find-replace expressions to a file: - -`sed -e 's/{{find}}/{{replace}}/' -e 's/{{find}}/{{replace}}/' {{filename}}` - -- Replace separator `/` by any other character not used in the find or replace patterns, e.g. `#`: - -`sed 's#{{find}}#{{replace}}#' {{filename}}` - -- [d]elete the line at the specific line number [i]n a file, overwriting the file: - -`sed -i '' '{{line_number}}d' {{filename}}`