Introduction to the search and replace of shell Script Learning guide _linux Shell

Source: Internet
Author: User
Tags posix

3.1 Find text
grep: Use POSIX-defined basic regular Expressions (BRE).
Egrep: Using an extended regular expression (ERE).
Fgrep: Fast grep. Use the optimized algorithm to match a fixed string rather than a regular expression.

The 1992 POSIX standard integrates these three revisions into a grep program.

$ who | Grep-f Austen
Use the-f option to find a fixed string. In fact, as long as the matching pattern does not contain the meta character of the regular expression,
The grep default behavior pattern is equivalent to using-F.

3.2.6 The text file to replace it.
In general, the correct procedure for performing text substitution should be the SED-stream editor.
Sed ' s/:.*//'/etc/passwd | Delete everything after the first colon
Sort-u Sort list and delete duplicate parts

Any character that can be displayed can be used as a delimiter.

Copy Code code as follows:

Sed ' s;/home/tolstoy/;/home/lt/; '
Sed ' s/\\/\\/g '

With the-E and-f options, you can replace more than one sed at a time without having to string through the pipes.

Copy Code code as follows:

$ Sed-e ' s/foo/bar/g '-e ' s/chicken/cow/g ' File1.xml > File2.xml
Or
$ cat fixup.sed
S/foo/bar/g
S/chicken/cow/g
...
$ sed-f fixup.sed file1.xml > File2.xml

The operation of 3.2.8 SED
Each file name on the command line opens and reads sequentially. If there are no files, standard input is used.
Sed reads each file, reads one line at a time, and places the read rows into one area of memory (the pattern space).
All edits are applied to the contents of the schema space, and when all is done, SED will take the pattern
The final content of the space is printed to the standard output, then back to the beginning, reading another input line.

3.3 Field Processing
With a Space (tab) or a specific delimiter (such as a colon).
#字符起始的行表示注释, the software must be able to ignore such lines.

The best example of separating fields with delimiters is/etc/passwd: A row represents a user, each field separated by a colon.
The file contains 7 fields:
Tolstoy:x:2076:10:leo Tolstoy:/home/tolstoy:/bin/bash
1. User name: 2. Password after encryption: 3. User id:4. User group Id:5. Name: 6. Root: 7. Login Shell.

3.3.2 Use Cut to select fields

Copy Code code as follows:

$ cut-d:-F 1,5/ETC/PASSWD
Root:root
...
Tolstoy:leo Tolstoy
$ cut-d:-F 6/ETC/PASSWD
/root
...
/home/tolstoy

3.3.3 using Join connection fields
Combine multiple files with a common key value (the primary field).
$ cat Sales
#业务员 Amount of data
Joe 100
Jane 200
Herman 150
Chris 300

$ cat Quotas
#业务员 quotas
Joe 50
Jane 75
Herman 80
Chris 95

Copy Code code as follows:

#! /bin/sh
# merge-sales.sh
# delete annotations and sort data files
Sed '/^#/d ' quotas | Sort > quotas.sorted
Sed '/^#/d ' sales | Sort > sales.sorted
# combined with the first key value
Join Quotas.sorted sales.sorted
# Delete cached files
RM quotas.sorted sales.sorted

3.3.4 use awk to rearrange fields

AWK Program Basic Schema: pattern {action}
Pattern is usually surrounded by slashes ere,action is usually a clear print statement.
Omitting pattern performs an action on each input record, and omitting the action is equivalent to {print}.

awk automatically divides records into fields and stores the number of fields in each record in the built-in variable NF.
The default is whitespace delimited, and you can set the FS variable to a different value. The $ plus number represents the field value.
awk ' {print '} ' prints the 1th field
awk ' {print $, $} ' prints fields 2nd and 5th
awk ' {print $, $NF} ' prints the first and last field
awk ' NF > 0 {print $} ' prints a non-empty line
awk ' NF > 0 ' ditto

Awk-f: ' {print $, $} '/etc/passwd set field separator characters, the-F option automatically sets the FS variable.
Root root
...
Tolstoy Leo Tolstoy

Remember to separate the parameters of the print with commas, or awk will connect all the adjacent values.
Awk-f: ' {print ' User ' is really ' $} '/etc/passwd
Userrootis Reallyroot
...
Usertolstoyis Reallyleo Tolstoy

Related Article

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.