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 Linux uses GNU sed

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces Linux how to use GNU sed, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The stream editor is used to read text from a file or to make basic changes from a pipe. The results are sent to standard output. The syntax of the sed command does not specify the output file, but the results can be written to the file by using output redirection. The editor does not change the original file.

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.confuniq 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-w6grep command

Grep searches the file (or command output) for the specified regular expression and outputs matching lines in standard output.

Sample

Displays information about the user gacanepa in the file / etc/passwd, ignoring case.

# grep-i gacanepa / etc/passwd how to use GNU sed on Linux and how to use GNU sed on Linux

Displays all the contents that begin with rc and follow any number under the / etc folder.

# ls-l / etc | skills for using grep rc [0-9] tr command

The tr command can be used to convert (change) or delete characters from standard input and write the results to standard output.

Sample

Change all lowercase to uppercase in the sortuniq.txt file.

# cat sortuniq.txt | tr [: lower:] [: upper:]

The delimiter in the compressed ls-l output is a space.

# ls-l | how to use tr-s''cut command

The cut command can extract partial input (from standard input or file) based on bytes (- b option), characters (- c), or fields (- f) and output the results to standard output. In the last case (field-based), the default field delimiter is a tab, but a different delimiter can be specified by the-d option.

Sample

Extract the user account and their assigned default shell from / etc/passwd (the-d option allows us to specify the delimiter, and the-f option specifies which fields will be extracted).

# cat/etc/passwd | cut-d:-f1,7

Combining the above commands, we will create a text stream using the first and third non-empty files in the output of the last command. We will use grep as the first filter to check the session of the user gacanepa, and then compress the delimiter to a space (tr-s'). Next, we will use cut to extract the first and third fields, and finally use the second field (in this example, the IP address) to sort it, and then use uniq to remove the duplicates.

# last | grep gacanepa | tr-s''| cut-d''-f1,3 | sort-k2 | uniq

The above command shows how to combine multiple commands and pipes to get the filtered data according to our requirements. You can also use it step by step to help you understand how output is transferred from one command to the next (by the way, this is a very good learning experience! )

Thank you for reading this article carefully. I hope the article "how to use GNU sed in Linux" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report