Linux Text Tool sed

Source: Internet
Author: User
Tags uuid vars

Sed


Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output.



SED usage:

sed [OPTION] ... ' Script ' [Input-file] ...


Common Options :     

-N: Silent mode (suppresses direct screen output if not matched, only shows results after script processing)
-E Script: Multi-point editing (multiple scripts can be run concurrently)
-F Script-file: reads the script file and runs (each line in the file has a matching condition)
-R: Supports extended regular expressions
-I: Edit source files directly


script:

Address Delimitation Edit Command


Address delimitation

1. Empty address: processing the full text

2. Single Address:

#: Specified line (#表示数字)

/pattern/: Each row that is matched by this pattern (matched by a regular expression)

3. Address range:

#,#: First line to first line

#,+#: Relative address notation (e.g. 3,+6 means 6 rows from line 3rd)

#,/pat1: Line from # # to match to pattern 1

/PAT1,/PAT2: Line starting from first mode to end of second mode

$: Last line

4. Step in:

~

One-to-one: all cardinality lines (starting at line 1th every 1 rows)

2~2: All even lines (starting at line 2nd every 1 rows)


Edit command

D: Delete

P: Display the contents of the mode space (if not add-N, the line that matches by default is shown again)

A \text: Append text "text" After line, support multi-row append using \ n

I \text: Insert text "text" in front of line, support using \ n to implement multiline insertion

C \text: Replace the matched line with the text "text" specified here

W/path/to/somefile: Save the pattern space to match the line to the specified file

R/path/from/somefile: Reads the contents of the specified file to the line after the current file is matched by the pattern; file merge

=: Prints line number for the line to which the pattern matches

!: Conditional Inversion

s///: Find replacement, its delimiter can be self-specified, commonly used [email protected]@@, s## #等 (s/find content/replace content/replacement tag)

Replace tag:

G: global substitution;

I: Ignore case

W/path/to/somefile: Saves the result of the substitution success to the specified file;

P: Shows the successful replacement line;



use of SED commands :

    1. Delete white space characters at the beginning of all lines in the/tmp/grub.cfg file (delete is to replace it with empty)

 First look at the/tmp/grub.cfg file [[email protected] ~]# cat /tmp/ grub.cfg## do not edit this file## it is automatically generated  by grub2-mkconfig using templates# from /etc/grub.d and settings  from /etc/default/grub#### begin /etc/grub.d/00_header ## #set  pager=1if [  -s  $prefix/grubenv ]; then  load_envfiif [  "${next_entry}"  ]  ; then   set default= "${next_entry}"    set next_entry=    save_env next_entry   set boot_once=trueelse   set  default= "${saved_entry}" 
After using the SED command [[email protected] ~]# sed ' s/^[[:space:]]\+//'/tmp/grub.cfg## do not EDIT this file## It's automatically ge Nerated by Grub2-mkconfig using templates# from/etc/grub.d and Settings from/etc/default/grub#### begin/etc/grub.d/00_h Eader # # #set pager=1if [-S $prefix/grubenv]; Thenload_envfiif ["${next_entry}"]; Thenset default= "${next_entry}" set next_entry=save_env next_entryset boot_once=trueelseset default= "${saved_entry}"



2. Delete all lines beginning with # in the/tmp/grub.cfg file, beginning with # and all whitespace characters in the back

First look at the/etc/grub.conf file [[email protected] ~]# cat/tmp/grub.cfg## do not EDIT this file## It's automatically generated by GR Ub2-mkconfig using templates# from/etc/grub.d and Settings from/etc/default/grub#
After operation using the SED command [[email protected] ~]# sed ' s/^#[[:space:]]//'/tmp/grub.cfg#do not EDIT this file#it is automatically Generat Ed by grub2-mkconfig using TEMPLATESFROM/ETC/GRUB.D and Settings from/etc/default/grub#



3. Take the even line in the/etc/fstab file and save as/tmp/newfstab

First look at the/etc/fstab file [[email protected] ~]# cat /etc/fstab## /etc/fstab# created  by anaconda on Fri May 13 14:28:49 2016## Accessible  filesystems, by reference, are maintained under  '/dev/disk ' # See man  pages fstab (5),  findfs (8),  mount (8)  and/or blkid (8)  for more  info#uuid=b66544bf-32a4-4bef-82cb-0059f9e94b34 /                        xfs      defaults        0 0uuid= 321571a3-d498-4677-be37-fc18793f3ca1 /boot                    xfs     defaults         0 0uuid=54e25c6d-d378-4c2f-9df4-62d25f6065f8 /usr                     xfs     defaults         0 0UUID=9b2e4d08-b266-47fc-b8d1-3f81b76094fe swap                     swap     defaults        0 0
 [[email protected] ~]# sed  ' 2~2w /after operation using sed command Tmp/newfstab '  /etc/fstab again to view the saved as/tmp/newfstab[[email protected] ~]# cat /tmp/newfstab  ## Created by anaconda on Fri May 13 14:28:49 2016#  accessible filesystems, by reference, are maintained under  '/dev/disk ' #UUID =321571a3-d498-4677-be37-fc18793f3ca1 /boot                    xfs     defaults         0 0UUID=9b2e4d08-b266-47fc-b8d1-3f81b76094fe swap                      Swap    defaults        0 0 



4.echo an absolute path to SED, taking its base name

After using the SED command (if you use s///, all paths need \ Translate, avoid trouble, use [email protected]@@) [email protected] vars]# echo/etc/yum/vars/| Sed ' [email protected][^/]\+/\[email protected]@ '/yum/vars/


Linux Text Tool sed

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.