Linux sed Command | Linux Stream EditorLinux 'sed' command stands for stream editor. It is used to edit streams (files) using regular expressions. But this editing is not permanent. It remains only in display, but in actual, file content remains the same. Primarily, it is used for text substitution; additionally, it can be used for other text manipulation operations like insert, delete, search, and more. The sed command allows us to edit files without opening them. Regular expression support makes it a more powerful text manipulation tool. Syntax:Options:The following are some command line options of the sed command: -n, --quiet, --silent: It forcefully allows us to print of pattern space. -e script, --expression=script: It is used to add the script to the commands to be executed. -f script-file, --file=script-file: It is used to add the contents of script-file to the commands to be executed. --follow-symlinks: it is used to follow symlinks when processing in place. -i[SUFFIX], --in-place[=SUFFIX]: it is used to edit files in place (creates backup if SUFFIX option is supplied). -l N, --line-length=N: It is used to specify the desired line-wrap length for the `l' command. --posix: it is used to disable all GNU extensions. -E, -r, --regexp-extended: It allows us to use the extended regular expressions in the script (for portability use POSIX -E). -s, --separate: it is used for considering files as separate rather than as a single and continues the long stream. --sandbox: It is used to operate in sandbox mode. -u, --unbuffered: It is used for loading the minimal amounts of data from the input files and flushes the output buffers more often. -z, --null-data: It is used to separate lines by NUL characters. --help: it is used to display the help manual. --version: It is used to display version information. Examples of sed CommandLet's see the following examples:
Applying to the STDIN directoryThe sed command is not just limited to manipulate files; also, we can apply it to the STDIN directory. The above commands will replace the first text with the second text pattern. Consider the below output: From the above output, first, we have performed 'sed' command on a string 'class7' where 'class' is changed into 'jtp' and 7 into 10. Then we have performed 'sed' command on a stream 'msg.txt' where 'learn' is converted into 'study.' Global ReplacementIn the earlier example, all 'learn' words were not edited into 'study'. To edit every word, we have to use a global replacement 'g'. It will edit all the specified words in a file or string. Syntax: Consider the below examples: The above commands will replace all the specified text pattern. Consider the below output: From the above output, by executing the command "echo class7 class9 | sed 's/class/jtp/g'" all the 'class' is converted into 'jtp' and with command "cat msg.txt | sed 's/learn/study/g'" all the 'learn' was converted into 'study'. Removing a LineThe 'd' option will let us remove a complete line from a file. We only need to specify a word from that line with 'd' option, and that line will be deleted. But, note that all the lines having that same word will be deleted. It will be executed as: Consider the below command: The above command will delete the lines having the word 'jtp'. Consider the below output: From the above output, by executing the command "cat msg.txt | sed '/jtp/d'" all lines containing the word 'jtp' are deleted. Using the Multiple sed CommandThe '-e' option allows us to execute the multiple sed commands at once. We can perform more than one sed operation by executing the command as: |