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

How to use GNU sed in Linux

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How is GNU sed used in Linux? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Working with text streams in Linux

Linux treats inputs and outputs in a program as character streams or character sequences. Before we begin to understand redirection and piping, we must first understand the three most important Imax O (input and output (Input and Output)) streams, which are, in fact, special files (data streams and peripherals (device files) are also considered normal files according to the conventions in UNIX and Linux).

The difference between > (redirect operator) and | (pipe operator) is that the former connects the command to the file, while the latter connects the output of the command to another command.

# command > file

# command1 | command2

Because the redirect operator silently creates or overwrites files, we must use it very carefully and never confuse it with pipes. The advantage of pipes on Linux and UNIX systems is that the output of the first command is not written to a file but is read directly by the second command.

In the following operational exercises, we will use this poem, "A happy child" (the author is unknown)

Use sed

Sed is an abbreviation for stream editor (stream editor). As an additional explanation for those who don't understand terminology, a stream editor is a tool for performing basic text transformations in an input stream (input in a file or pipe).

The most basic use of sed is character substitution. We will start by rewriting each occurrence of lowercase y to uppercase Y and redirecting the output to ahappychild2.txt. The g flag indicates that sed should replace all instances that should be replaced on each line of the file. If this flag is omitted, sed will only replace the first instance in each line

Basic syntax:

# sed's/term/replacement/flag'file

Our example:

# sed's/y/Y/g' ahappychild.txt > ahappychild2.txt

If you want to search for or replace special characters (such as /,\, &) in alternate text, you need to escape it with a backslash.

For example, we will replace a text with a symbol, while we will replace the first I that appears at the beginning of the line with You.

# sed's/and/\ & / gsmarts / ^ I / You/g' ahappychild.txt

In the above command, it is well known that ^ (caret) is the symbol used to indicate the beginning of a line in a regular expression.

As you can see, we can connect two or more replacement commands (and use regular expressions in them) by separating them with semicolons and wrapping them with parentheses.

Another use of sed is to display or delete selected portions of the file. In the following example, the first five lines from June 8th in / var/log/messages will be displayed.

# sed-n'/ ^ Jun 8 / p'/var/log/messages | sed-n 1Pie5p

Note that by default, sed prints each line. We can override this behavior with the-n option and tell sed that we only need to print (represented by p) the matching portion of the file (or pipe) (lines starting with "Jun 8" are specified in the first command and one to five lines in the second command).

Finally, it may be a useful trick to keep the file itself and delete comments when checking the script or configuration file. The following single-line sed command removes (d) a blank line or a line that begins with # (| the character performs a Boolean OR on two regular expressions).

# sed'/ ^ #\ | ^ $/ d 'apache2.conf

Uniq command

The uniq command allows us to return or delete duplicate lines in the file, writing to standard output by default. We must note that the uniq command does not delete two duplicate lines unless they are adjacent to each other. As a result, uniq is often used in conjunction with a pre-sort command, an algorithm for sorting lines of text. By default, sort uses the first field (separated by a space) as the key field. To specify a different key field, we need to use the-k option.

Sample

The du-sch / path/to/directory/* command returns the disk space usage of each subfolder and file under the specified directory in a human-readable format (and also shows the overall situation of each directory), and outputs by size, but by the name of the subfolder and file. We can use the following command to sort it through size.

# du-sch / var/* | sort-h

You can count the number of log events by using the following command to tell uniq to compare the first six characters (- w 6) (in this case, the specified date) of each line, and output the number of occurrences (- c) at the beginning of each line.

# cat/var/log/mail.log | uniq-c-W6

After reading the above, have you mastered how GNU sed is used in Linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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