Introduction to searching and replacing Shell script Learning Guide

Source: Internet
Author: User

3.1 search for text
Grep: use the basic regular expression (BRE) defined by POSIX ).
Egrep: use an extended regular expression (ERE ).
Fgrep: Quick grep. Use an optimized algorithm to match a fixed string instead of a regular expression.

1992 POSIX standards integrate 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 mode does not contain the meta characters of the regular expression,
The default grep behavior mode is equivalent to using-F.

3.2.6 replace in a text file
In general, the correct program for executing text replacement should be sed-stream editor.
Sed's/:. * // '/etc/passwd | delete everything after the first colon
Sort-u sort list and delete duplicate parts

Any printable characters can be used as delimiters.

Copy codeThe Code is as follows: sed's;/home/tolstoy/;/home/lt /;'
Sed's/\ & bsol;/G'

With the-e and-f options, you can replace multiple sed streams at a time without concatenating them through pipelines.

Copy codeThe Code is 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

3.2.8 sed operations
Each file name on the command line is opened and read in sequence. If no file exists, standard input is used.
Sed reads each file, reads one row at a time, and places the read row in a memory area (mode space ).
All the operations on the editing are applied to the content of the mode space. After all the operations are completed, sed applies the Mode
Print the final content of the space to the standard output and return to the start point to read another input line.

3.3 Field Processing
Use spaces (tabs) or specific delimiters (such as colons ).
# The line starting with a character indicates a comment. The software must ignore this line.

The best example of fields separated by delimiters is/etc/passwd: a row represents a user, and each field is separated by a colon.
This file contains seven fields:
Tolstoy: x: 2076: 10: Leo Tolstoy:/home/tolstoy:/bin/bash
1. User name: 2. encrypted password: 3. User ID: 4. User Group ID: 5. Name: 6. root directory: 7. logon Shell.

3.3.2 use cut to select fields

Copy codeThe Code is 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 join fields using join
Combine multiple files with a common key value (primary field.
$ Cat sales
# Sales volume
Joe 100
Jane 200
Herman 150
Chris 300

$ Cat quotas
# Salesperson quota
Joe 50
Jane 75
Herman 80
Chris 95

Copy codeThe Code is as follows :#! /Bin/sh
# Merge-sales.sh
# Delete comments and sort data files
Sed '/^ #/d' quotas | sort> quotas. sorted
Sed '/^ #/d' sales | sort> sales. sorted
# Combine with the first key value
Join quotas. sorted sales. sorted
# Deleting cached files
Rm quotas. sorted sales. sorted

3.3.4 use awk to re-orchestrate Fields

Basic architecture of the awk program: pattern {action}
Pattern is usually the ERE enclosed by a slash, and action is usually a clear print statement.
If pattern is omitted, an action is executed for each input record. If pattern is omitted, it is equivalent to {print }.

Awk automatically divides each record into fields, and stores the number of fields in each record to the built-in variable NF.
By default, the values are separated by spaces. You can also set the FS variable to a different value. $ Add a number to indicate the field value.
Awk '{print $1}' prints 1st Fields
Awk '{print $2, $5}' print 2nd and 5th Fields
Awk '{print $1, $ NF}' prints the first and last Fields
Awk 'nf> 0 {print $0} 'print non-empty rows
Awk 'nf> 0' is the same as above

Awk-F: '{print $1, $5}'/etc/passwd sets the field delimiter. The-F option automatically sets the FS variable.
Root
...
Tolstoy Leo Tolstoy

Remember to separate print parameters with commas. Otherwise, awk connects all adjacent values.
Awk-F: '{print "User" $1 "is really" $5}'/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.