Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of grep, sed and awk operated by Linux payment Command

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of grep, sed and awk about the operation of Linux payment commands. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. Grep

The grep command is mainly used to find text content. It supports regular expression lookup, and the command format is:

Grep [option] pattern filename

For example: find the line that contains "text" in the filename text:

Grep "text" filename

This command outputs only matching lines of text by default

When option is-o, the command line only outputs matching text when option is-v, and the command line only outputs unmatched text lines when option is-R-r, all files in the matching directory are matched. II. Sed

The sed command is mainly used for editing text content. By default, only the schema space is processed, not the original data, and sed is processed for row-by-row data.

The command format for sed is:

Sed [option] 'command' filename

Common options for option are as follows:

-n: use silent mode. In the general use of sed, all data from stdin is generally listed on the terminal. But if you add the-n argument, only the row (or action) that has been specially handled by sed will be listed. -e: edit sed actions directly on the command line mode. -I: directly modify the contents of the read file instead of outputting it to the terminal.

Command can be divided into the following categories:

A: append, a can be followed by strings, and these strings will appear on a new line (current next line) I: insert, I can be followed by strings, and these strings will appear on a new line (current previous line) d: delete in behavior unit c: replace in behavior unit, c can be followed by string s: search in line and replace p: display in behavior unit Usually p runs with the parameter sed-n

For example:

1. Append hello world to the last line of the filename text:

Sed'$a hello world' filename

2. Insert hello world in the first line of filename text:

Sed'1i hello world' filename

3. Append hello world to the last line and insert hello world on the first line:

Sed-e'$a hello world'-e'1i hello world' filename

In addition, sed is more commonly used is text replacement, it also supports regular expressions, powerful. For example:

1. Replace the oldstring in each line of filename text with newstring:

Sed's Universe OldstringCharger newstringUniverse g'filename

2. Delete a blank line:

Sed'/ ^\ s salary filename

PS:\ s in regular expressions represents white space characters (including spaces, tabs, etc.)

III. Awk

The awk command is mainly used for the analysis and processing of text content.

If you need to generate information such as reports for the data you are working with, or if the data you are working with is processed in columns, it is best to use awk.

Awk reads in a record separated by the'\ n' newline character, and then divides the record into fields by the specified domain delimiter. $0 represents all fields, $1 represents the first field, and $n represents the nth field.

For example: separate each line of filename text with ":" and print the first column

Awk-F':'{print $1} 'filename

Printing can use the print function, if you need to format printing, it is similar to the C language using the printf function.

Exercise: examples of customized displays for sed and awk

1. You can make a text test.txt with the following contents:

This is my cat, my cat's name is bettyThis is my dog, my dog's name is frankThis is my fish, my fish's name is georgeThis is my goat, my goat's name is adam

The results that need to be displayed are:

Cat:bettydog:frankfish:georgegoat:adam

If you use sed, you can enter

Sed 's/This is my\ (. *\),. * is\ (. *\) /\ 1:\ 2Accord g 'test.txt

If you use awk, there are two ways

Awk-F'[,]'{print $4 OFS= 10} OFS= ":" test.txtawk-F'[,]'{printf ("% SV% s\ n", $4 camera 10)} 'test.txtawk-F,' {print $1 camera 2} 'test.txt | awk' {print $4 camera 9} 'OFS= ":" sample analysis of grep, sed and awk for Linux payment command operations "ends here Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report