The sed
command uses scripts to process text files. It can process and edit text files according to the instructions in the script. It is mainly used for automatically editing one or more files, simplifying repetitive operations on files, and writing conversion programs, among other things.
-n, --quiet, --silent
: Suppress automatic printing of pattern space.-e script, --expression=script
: Add the script to the commands to be executed.-f script-file, --file=script-file
: Add the contents of the script file to the commands to be executed.--follow-symlinks
: Follow symbolic links when doing in-place editing.-i[SUFFIX], --in-place[=SUFFIX]
: Edit files in place; if a suffix is provided, make a backup of each edited file.-l N, --line-length=N
: Specify the desired line length N
for the l
command.--POSIX
: Disable all GNU extensions.-r, --regexp-extended
: Use extended regular expressions in the script.-s, --separate
: Treat input files as separate files, not a single continuous long stream.-u, --unbuffered
: Load minimal amounts of input from the files and flush the output buffer more often.--help
: Display help information.--version
: Display version information.The content of the file.txt
file is as follows.
Replace the first occurrence of the word unix
with linux
in each line of the file. If you want to save the changes, you need to use output redirection.
Replace the second occurrence of the word unix
with linux
in each line.
Use the /g
replacement flag to globally replace the specified sed
command to replace all occurrences of the string in each line.
Replace all occurrences of the pattern past the second occurrence.
Enclose the first character of each word in parentheses and print it inside the parentheses.
You can restrict the sed
command to replace the string on a specific line number.