sed: refresh/add pages (#7931)

pull/1/head
Emily Grace Seville 2022-11-12 23:05:16 +10:00 committed by GitHub
parent 9d9774952d
commit eefe5a6c52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 58 deletions

View File

@ -1,36 +1,25 @@
# sed
> Edit text in a scriptable manner.
> See also: `awk`, `ed`.
> More information: <https://www.gnu.org/software/sed/manual/sed.html>.
- 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}}`

View File

@ -1,36 +0,0 @@
# sed
> Edit text in a scriptable manner.
> More information: <https://ss64.com/osx/sed.html>.
- 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}}`