Shell: Regular expressions and text processing

Source: Internet
Author: User

Regular expressions

Job One: organize regular expression Blogs
Regular expressions
A regular expression is the rule of a string of characters in a class word.
Grep
The grep command is useful in situations where a pattern-based search is performed on the contents of one or more files.
A pattern can be a single character, multiple characters, a single word, or a sentence.
Format
grep [Options]

Main Parameters
[Options] main parameters:
-C: Outputs only the count of matching rows.
- I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
pattern Regular Expression main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: Matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: a single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
. : all the individual characters.
*: There are characters, the length can be 0.


the regular introduction

^ Beginning of the line
$ end of line
. Any single character other than the line break
* 0 or more of the leading characters
. * All characters
[] Any character within a group of characters
[^] reverse each character within a character group (does not match each character in a group of characters)
^[^] lines that begin with characters within a non-character group
[A-z] lowercase letter
[A-z] capital letter
[A-z] lowercase and uppercase letters
[0-9] Number
\< words are usually separated by spaces or special characters, and successive strings are treated as words .
\> Word Tail


job Two: grep jobs (regular expressions and character processing)
target file/etc/passwd, using the grep command or Egrep
1. All rows containing root are displayed:
2. Output any rows that contain bash, and also output the contents of the next two lines, followed by the row:
3. Shows how many lines contain nologin.
4. Shows that the rows contain root and the line number is output.
5. Displays the file in the
6. New User
Abominable
abominate
anomie
atomize
write regular expressions and match them up
7. Built four users
ALEX213SB
wpq2222b
Yh438pig
egon666
Egon

Filter out the user name consists of a letter + number + letter Line


8. Displays all filenames containing root in/etc directory


9. Filter out all comments and all blank lines in/etc/ssh/sshd_config


Job Three: SED job:/etc/passwd file as Template

1. Delete the first character of each line of the file.

2. Delete the second character of each line of the file.

3. Delete the last character of each line of the file.

4. Delete the second-to-last character of each line of the file.

5. Delete the second word in each line of the file.

6. Delete the second-to-last word in each line of a file.

7. Delete the last word in each line of the file.

8, swaps the first character and the second character of each line.

9, swaps the first character and the second word of each line.

10, swap the first word and the last word of each line.

11, delete all the numbers in a file.

12, remove all spaces at the beginning of each line.

13, replace any spaces that appear in the file with tabs.

14, enclose all uppercase letters in parentheses ().
Sed-r ' s/[a-z]/(&)/g '/etc/passwd
15, print 3 times per line.

16, only the first word of each line is displayed.

17, print the first word and the third word of each line.

18, use the command to get the format MM/YY/DD date format, combined with the pipeline, change it to MM;YY;DD format

Shell: Regular expressions and text processing

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.