Linux sed command

Source: Internet
Author: User
Tags posix

sed is a very good file processing tool, itself is a pipeline command, mainly in the behavior of the unit processing, the data row to replace, delete, add, select and so on. Sed-Help view the use of SED-N,--quiet,--Silent Suppress automatic printing of the pattern space blocks data in the default output (display) template spaces, at which point only the content specified by the p command is output. -E script,--expression=Script Add the script to the commands to be executed adds the command in the specified script scripts to the command group that processes the input file-F Script-file,--file=script-file Add the contents of script-the file to the commands-be executed will specify script scripts-the commands in file are added to the command group that processes the input files. --follow-symlinks follow symlinks when processinginchPlace is only available in systems that support symbolic connections, and the option-I only have effect: if the specified input file is a symbolic connection, SED will process the target file of the symbolic connection and remain connected. The default SED disconnects so that the destination file for the symbolic connection is not modified. -i[suffix],--inch-place[=suffix] Edit filesinchPlace (makes backupifSUFFIX supplied) place the file in the-place in the editing process. GNU is implemented by creating a temporary file and sending the output of the SED to this file instead of sending it to standard output. Apply to command: =, a, c, I, L, p, but also command W or W and specify output file as/dev/stdout, the output of SED is also sent to standard output. Another option is implied by this option-S. There is no space between I and suffix when a file is processed, SED creates a temporary file name that is the original name of the output file (that is, the input filename being processed). If a suffix suffix is specified, the old file is replaced with a suffix suffix and the old file is overwritten without a backup. The name change rule is: If suffix does not contain an asterisk (*), append suffix as a suffix directly after the old file name; otherwise, each asterisk in the suffix (*) are replaced by the old file name. The advantage is that you can also specify a prefix for the backup file, not just the suffix, or even put the backup in another directory (the directory must already exist)-L N,--line-length=n specify the desired line-wrap length forThe ' L'command--posix Disable all GNU extensions.specifying the L (L) command wrap length to n,0 means that the long line is not wrapped in a line, and the default is 70. -R,--regexp-extended use extended regular expressionsinchthe script. Use the extended regular expression rule to interpret the matching template. -S,--separate consider files as separate rather than as a single continuous long stream. The default SED considers multiple input files specified on the command line to be processed as a continuous long stream of data. With this GNU extension option, SED will be treated as separate files, but note that the specified range of addresses cannot span other files; the line numbers displayed are relative to the first line of the respective file; The character $ indicates the last line of each file The call file from the R command will be re-executed once for each input file. -U,--unbuffered load minimal amounts of data fromThe input files andflush the output buffers more often the input and output are based on the actual, as small as possible buffer processing, or do not buffer. The input to the SED command comes from something like command tail–f, and you want to see the processed output as quickly as possible, which is especially useful. -Z,--null-data separate lines by NUL characters--help Display this Help andExit displays sed parameter information. --version Output Version Information andExit Displays the SED version information after exiting. Intercept data from line fifth: sed-N'5p'/etc/passwd multiple spaces into one space: [email protected]:/home/jianeng#Tail Test.txtHow is you! fine! Thank you! [Email protected]:/home/jianeng#sed ' s/\ */\/g ' test.txtHow is you! fine! Thank you! Delete the data behind the man: [email protected]:/home/jianeng#head/etc/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologinsys:x:3:3:sys:/dev:/usr/sbin/nologinsync:x:4:65534:sync:/bin:/bin/syncgames:x:5:60:games:/usr/games:/usr/sbin/nologinman:x:6:12:man:/var/cache/man:/usr/sbin/nologinlp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologinmail:x:8:8:mail:/var/mail:/usr/sbin/nologinnews:x:9:9:news:/var/spool/news:/usr/sbin/Nologin[email protected]:/home/jianeng#head/etc/passwd | grep ' man: ' | sed ' s/:.*//g 'Man (:.*This means: All characters after this character a.*b, it will match the longest string starting with a and ending with B)

Sed is a very good file processing tool, itself is a pipeline command, mainly in the behavior of the unit processing, the data row to replace, delete, add, select and so on.
Sed-help viewing the use of SED
-N,--quiet,--silent suppress automatic printing of pattern space blocks the data in the default output (display) template spaces, at which point only the content specified by the p command is output.
-e script,--expression=script Add the script to the commands to be executed adds the command in the specified script scripts to the command group that processes the input file
-F Script-file,--file=script-file Add the contents of Script-file to the commands to be executed adds the command in the specified script script-file to the processing Enter the file in the command group. --follow-symlinks follow symlinks when the processing in place only works in systems that support symbolic connections, and with option-I at the same time: if the specified input file is a symbolic connection, SED handles the target file of the symbolic connection and remains connected. The default SED disconnects so that the destination file for the symbolic connection is not modified.
-i[suffix],--in-place[=suffix] edit files in place (makes backup if SUFFIX supplied) put the file in In-place edit processing. GNU is implemented by creating a temporary file and sending the output of the SED to this file instead of sending it to standard output. Applies to the command: =, a, c, I, L, p, but also the command W or W and the output file is specified as/dev/stdout, the output of SED is also sent to the standard output. This option has implied another option-S. There is no space between I and suffix when a file is processed, SED creates a temporary file name that is the original name of the output file (that is, the input filename being processed). If a suffix suffix is specified, the old file is replaced with a suffix suffix and the old file is overwritten without a backup. The rule of change is: If suffix does not contain an asterisk (*), append suffix as a suffix directly after the old file name, otherwise, each asterisk (*) in suffix is replaced by the old file name. The advantage is that you can also specify a prefix for the backup file, not just the suffix, or even put the backup in another directory (the directory must already exist)
-L N,--line-length=n specify the desired line-wrap length for the ' l ' command--posix Disable all GNU extensions. Specifying the L (L) command wrap length to n,0 means that the long line is not wrapped in a line, and the default is 70.
-R,--regexp-extended use extended regular expressions in the script. An extended regular expression rule is used to interpret the matching template.
-S,--separate consider files as separate rather than as a single continuous long stream. The default SED considers multiple input files specified on the command line as a continuous, long stream of data. With this GNU extension option, SED will be treated as separate files, but note that the specified range of addresses cannot span other files; the line numbers displayed are relative to the first line of the respective file; The character $ indicates the last line of each file The call file from the R command will be re-executed once for each input file.
-U,--unbuffered load minimal amounts of data from the input files and flush the output buffers more often on the input and output are based on the actual, as far as possible Small to buffer, or not to buffer. The input to the SED command comes from something like command tail–f, and you want to see the processed output as quickly as possible, which is especially useful.
-Z,--null-data separate lines by NUL characters--help Display this Help and exit displays sed parameter information after exiting. --version output version information and exit displays the SED version information after exiting.
Intercept data on line fifth: Sed-n ' 5p '/etc/passwd
Replace multiple spaces with a single space: [Email protected]:/home/jianeng# tail test.txt How is you! fine! Thank you!
[Email protected]:/home/jianeng# sed ' s/\ */\/g ' test.txthow is you! fine! Thank you!
Delete the data behind the man: [Email protected]:/home/jianeng# head/etc/passwdroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/ Usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologinsys:x:3:3:sys:/dev:/usr/sbin/nologinsync:x : 4:65534:sync:/bin:/bin/syncgames:x:5:60:games:/usr/games:/usr/sbin/nologinman:x:6:12:man:/var/cache/man:/usr/ Sbin/nologinlp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologinmail:x:8:8:mail:/var/mail:/usr/sbin/nologinnews:x:9:9: News:/var/spool/news:/usr/sbin/nologin
[Email protected]:/home/jianeng# head/etc/passwd | grep ' Man: ' | Sed ' s/:.*//g ' man (:. * means: All characters after this character are a.*b, it will match the longest string starting with a and ending with B)

Linux sed command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.