Learning Shell together (3) search and replace

Source: Internet
Author: User
Tags printable characters egrep

Learning Shell together (3) search and replace
Description: Regular Expressions can provide more powerful markup methods to match various actual numbers of texts with a single expression. The weapon strength of the grep family. ---------------------------- Grep: text matching program egrep: Use extended regular expressions to consume more computing resources fgrep: Fast grep, match fixed strings instead of regular expressions. ----------------------------- Who | grep-F root note:-F is the default grep option. to list the information of root login before a ticket, the information printed by who is handed over to grep, grep searches for the root row. Then print it out. Grep option file purpose: display one or more text lines in the mode, usually used for pipeline | the first step. For more information about the parameters, see. I don't need to talk about this. It has a lot of usage and various searches. It makes up a powerful Chen Ying with sed awk cut. If you study it together, you will certainly get more. Next, find a Shell for analysis. Regular Expression: a regular expression that allows you to search for the text of a matching criterion. For example, the expression matches multiple characters starting with ". To put it bluntly, it is to define Internal commands in a specific language, just as if you are communicating with the villagers in Beijing, and others do not understand. You can execute relevant commands. However, IT personnel like to get professional terms to better express their opinions. Regular Expressions are created from two basic components. General characters and special characters are meaningless characters. special characters are just like your hometown dialect, also known as metacharacters. Regular Expressions support the following commands: Query related: grep egrep agrep Editor: sed string processing program: awk icon perl python ruby tcl file viewing program: more page pg less file editing program: vi emacs jed jove vile vim and other three steps for writing regular expressions: 1. Know the content to be matched and how it appears in the text. 2. Write a pattern to describe the content to be matched. 3. The test mode is used to view the results of the regular expression that matches the content: when Hit hits Misses and does not Hit omissions, false alarms false hits vulgar. In other words, you need your needs and design requirements first. In the end, let's preview whether it is the same as what you need. There are only a few consequences. You can check the error. View the metadata graph in bytes ----------------------------------------- after reading the table, the rest is more exercises. You must first check the table based on the previous priority before viewing it. Otherwise, you will be dizzy. Package A small framework in a large framework, and wait for sed to include it. I will write more instances. Priority: high to low [...] [:] square brackets [] square brackets expressions \(\), otherwise, you cannot query the table. Stream editor Sed explanation: Regular Expressions and text replacement are supported for handling general file replacement in shell. Any printable characters can be used as delimiters, generally, punctuation marks are used as delimiters. For example, sed's/a/B/'file is used to replace the first a in the file with B. What is the meaning of the delimiters at the top? After reading this, you will understand that sed's/a/B/'file is equivalent to sed's # s # a # B 'file delimiters, thanks to the guidance of dn833, both sed and sed can be used, which means sed "s/a/B/'file can also be used. Usage: [address] command Replace: [address] s/pattern/replacement/flags flag flages:-n can be 1-512, indicates that the replacement condition g is changed globally for the nth occurrence, that is, the g after sed's/a/B/G' file, that is, flages all are replaced. There are many examples of Writing content between p print Mode w file into a file: ------------------------------------------------------ view the file

[root@localhost sed]# cat filea a a a aa b b b b bbbc c c c c cddd dd d d d d

 

Replace the first 'No space a' with B
[root@localhost sed]# sed 's/a/b/' file b a a a ab b b b b bbbc c c c c cddd dd d d d d

 

Replace all a with B g to replace all
[root@localhost sed]# sed 's/a/b/g' file b b b b bb b b b b bbbc c c c c cddd dd d d d d

 

Replace the first 'space a' and 'space C', where; is the Separator
[root@localhost sed]# sed 's/ a/, b/ ; s/ c/,d/' filea, b a a aa b b b b bbbc,d c c c cddd dd d d d d--------------------------------------------

 

Replacing '"[] \/. and other symbols is a bit different
-------------------------------------------[root@localhost sed]# sed 's/'\''/b/g' filea a a a aa b b b b bbbc c c c c cddd dd d d d da a aa aab " " " "" ""\\\\//[]b bb bb b b[root@localhost sed]# 

 

Replace other [] <>
[root@localhost sed]# sed 's/\[/b/g' filea a a a aa b b b b bbbc c c c c cddd dd d d d da a aa aa' " " " "" ""\\\\//b]' '' '' ' '[root@localhost ~]# 

 

RegEx example:
[root@localhost sed]# cat za:bc:de:f[root@localhost sed]# sed 's/\(.*\):\(.*\)/\2:\1/' zb:ad:cf:e

 

How can we see this regular expression? Sed's/\ (. * \): \ (. * \)/\ 2: \ 1/'z first check the big box sed's /? A /? B/'can be divided into two parts ,? A = \(.*\):\(.*\)? B = \ 2: \ 1. Can I see it? Why? A, \) is the related meaning. The positive value is changed to the negative value, so here we need to understand \ (, replace General characters (with special characters to form a regular expression, they are all the same ,. * after reading the table, you will find that. is any character, * is any number of first characters ,. * It can contain any number of characters. How can this problem be solved? B \ 2: \ 1 previously mentioned that the \ Positive is reversed. In sed, both 2 and 1 are general characters. Here, in turn, they are special characters, that is, the first and second paragraphs, what is the second paragraph? The one behind a. * \ 1 is the one before. Check the regular expression first. Check the big frame and check the priority. After reading more, you can see it. In addition to the empty lines and the line with the # number, I can write it myself using sed-e.
[root@localhost sed]# sed '/^$/D' /etc/vsftpd/vsftpd.conf |sed '/\#.*/D'anonymous_enable=YESlocal_enable=YESwrite_enable=YESlocal_umask=022dirmessage_enable=YESxferlog_enable=YESconnect_from_port_20=YESxferlog_std_format=YESlisten=YESpam_service_name=vsftpduserlist_enable=YES

 

Sed '/^ $/D'/etc/vsftpd. conf | sed '/\#. */d' split again, and sed '// D is the method for deleting a book. For details, see sed table. ^ $ ^ Starts with this, and $ ends with this. The beginning and end are empty rows. #. * # Is not used in sed or regular expressions, so #. * can also be used. We can better write ^. This is very important in the configuration file. If someone else wants to see your configuration file, would you give it hundreds of lines to others?
Sed-e mode [root @ localhost sed] # sed-e '/^ $/d'-E '/\#. */D'/etc/vsftpd. conf anonymous_enable = YESlocal_enable = YESwrite_enable = YESlocal_umask = Enabled = YESxferlog_enable = YESconnect_from_port_20 = Enabled = YESlisten = YESpam_service_name = Enabled = YES

 

Replace a single data with a single letter. This is not a global match. 1 and a 2b 3c. [Root @ localhost sed] # cat number123456123654 [root @ localhost sed] # sed 'y/123456/abcdef/'Number abcdefabcfed :: d command sed '2d 'file except file second line sed '2, $ d 'file: Delete the second row to the last row of sed '$ d' file, except the last row of sed'/test/'d file, except the row where the file contains test. Sed '/a/B/'d file except the rows between a and B. If you have mastered the rules, then you can change the instance. In fact, it has not changed. -------------------------------------------- CUT: used to CUT the data in a text file, it can be a field type and character type, a tabulation character is a single character. Usage: cut-c list [file...] cut-f list [-d delim] [file...] -c is dominated by characters-d delimiters-f is dominated by characters. Example: cut-d:-f/etc/passwd: 1st and 5th delimiters are printed. the/etc/passwd file contains too much content. ------------------------------------- [Root @ localhost sed] # echo 1: 2: a: e: l: 6 | cut-d:-f 1,41: eroot @ localhost sed] # explain JOin: multiple files can be combined. Each record in each file shares a key value, which indicates the primary field. Syntax: join [options...] file1 file2 conversion process is as follows:
----------------------------[root@localhost sed]# cat 1a 1 b 2c 4d 4[root@localhost sed]# cat 2a 2b 4c 8d 9[root@localhost sed]# join 1 2a 1  2b 2 4c 4 8d 4 9[root@localhost sed]# ----------------------------------

 

This data merging is good. Accumulate commands. If you accumulate more, you are a treasure. Awk explanation: read each file specified on the command (if not, the standard input), read a record at a time (line) then the command specified for each line of application .... | Awk '{print some-stuff}' |... sed and awk are a big topic that cannot be mentioned at a moment. Therefore, the original book only talked about a part of it. Awk '{print $1}' print the first program awk '{print $1, $5}' print 2-5 fields awk '{print $1, $ NFS} 'print 1 till the end of the print. Do not press ENTER awk 'nf> 0 {print $0} '. print 1 till the end of the print. You can press ENTER awk-F: '{print $1, $5}' to print the first and fifth fields as the delimiters. Is it the same as cut? Let's try it.
-----------------------------[root@localhost sed]# echo "a:b:c:d:e:f" |awk -F: '{print $1,$4}'a d[root@localhost sed]# echo "a:b:c:d:e:f" |cut -d: -f 1,4a:d---------------------------------

 

There is basically no difference, but there is a number in the middle, which will be discussed below.
Awk-F:-v 'ofs = ** ''print $1, $ 4' file is a ** separator. Let's try again. ------------------------- [Root @ localhost sed] # echo "a: B: c: d: e: f" | awk-F:-v 'ofs =: ''{print $1, $4} 'a: d [root @ localhost sed] # ----------------------------------------

 

Note that OFS =: 'is separated by a space and.

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.