💾 Archived View for darknesscode.xyz › notes › sed-command.gmi captured on 2024-07-09 at 00:28:37. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-05)

-=-=-=-=-=-=-

Sed Command Examples

SED command in UNIX is stands for stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion.

Syntax

sed OPTIONS... [SCRIPT] [INPUTFILE...]

Examplas

Here is just a brief list of examples of how to use sed

$sed 's/oldtext/newtext/' filemane.txt

Here the “s” specifies the substitution operation. The “/” are delimiters.

Replacing all the occurrence of the pattern in a line

$sed 's/oldtext/newtext/g' filename.txt

Parenthesize first character of each word

$ echo "This Is My Awesome Title" | sed 's/\(\b[A-Z]\)/\(\1\)/g'

Replacing string on a specific line number

$sed '3 s/oldtext/newtext/' filename.txt

Printing only the replaced lines use the -n option along with the /p print flag to display only the replaced lines.

$sed -n 's/oldtext/newtext/p' filename.txt

Replacing string on a range of lines

$sed '1,3 s/oldtext/newtext/' filename.txt

Deleting lines from a particular file

Examples:

Delete a particular line

$ sed '5d' filename.txt

Delete a last line

$ sed '$d' filename.txt

Delete from nth to last line

$ sed '12,$d' filename.txt

Delete pattern matching line

$ sed '/pattern/d' filename.txt

Learn more at

GNU Sed Manual

----------

Home

Linux

Notes

MicroLog

----------

© DarknessCode