sed
GNU docs -> https://www.gnu.org/software/sed/manual/sed.html
Stand input = standard input stream, etc…
Used for basic transformation on streams
type -a sed👉 sed is command, it is stand alone utility and not a shell built inman sed👉 to view documentationSubstitute command
sed "s/[search-pattern]/replacement-string/" [File to execute this command on]- it does not alter the content of the file yet, it only send the output to STDout
case sensitive my default
iflag to ignore string, usagesed 's/search-pattern/replacement-string/i' [filename]- we also use capitalI.
In case of multi line text it searches for text line by line and replaces its first match and then moves on
that is if there are two matches in a line it will replace only the first match
g- global replace to replace all the matchessed ’s/search-pattern/replacement-string/g' [filename]If we want only a particular match to be replace we can use the number flag like 1, 2, 3…etc
we can write to new file using the
>operation-i: for inline editing. Append something to-iflag to create a backup file and replace inline for original file. Example:sed -i.bak 's/search-pattern/replacement-string/' [filename]
Any character following the
scan be used as delimiter- ex:
sed ’s#/home/json/#/something#, here#is used a delimiter.
- ex:
Delete the content using
dcommand- to delete the line containing this -
sed ‘/this/d’ [filename]
- to delete the line containing this -
Multiple sed
seperate each with semicolon -
sed ‘/^#/d ; /^$/d' [filename]using
-eoption one for each sed operationsed -e ‘/^#/d’ -e ‘/^$/d’ [filename]
Run sed command from a file: (a file has all the sed commands we want to execute)
- Example scripts.sed file to be
/^#/d
/^$/d
We can run sed from this file using
-fflag -sed -f script.sed [filename to execute sed on]To execute only against single line in the file
sed ‘2 s/apahe/httpd/‘ [filename]