💾 Archived View for darknesscode.xyz › notes › sed-command.gmi captured on 2023-11-04 at 11:31:37. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2021-12-05)
-=-=-=-=-=-=-
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.
sed OPTIONS... [SCRIPT] [INPUTFILE...]
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
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
----------
----------
© DarknessCode