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 Sed text Editor in linux

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

Share

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

This article is about how to use the Sed text editor in linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Sed lacks the usual text box and writes directly to the file at the user's command.

The sed command was created for version 7 of AT&T 's original Unix operating system, and since then it is likely to be included in every Unix and Linux operating system. The sed application is a stream editor, and unlike a text editor, it does not open a visual buffer into which the file's data is loaded for processing. Instead, it operates on the file line by line based on a series of commands entered at the terminal or in the script.

Installation

If you are using Linux, BSD, or macOS, you have already installed GNU or BSD version of sed. This is a reimplementation of two different original sed commands, although they are similar, but with some small differences. GNU sed is generally considered to be the most feature-rich sed, and it can be widely used on these platforms.

If you can't find GNU sed (commonly known as gsed on non-Linux systems), you can download its source code from the GNU website. The advantage of installing GNU sed is that you can use its extra features, but it can also be limited to conforming to sed's POSIX specification if you need portability.

On Windows, you can use Chocolatey to install GNU sed.

How Sed works

The sed application processes only one row at a time. Because it has no visual display, it creates a pattern space in memory: a space containing the current line of the input stream (excluding any trailing newline characters). Once the pattern space is filled, your instructions to sed will be executed. Sometimes your instructions are conditional and sometimes unconditional, so the result of these instructions depends on how you use sed.

When the command ends, sed prints the contents of the pattern space to the output stream. The default output stream is standard output, but you can redirect it to a file, or even to the same file using the-- in-place=.bak option.

Then start the loop from the next input line.

The syntax of the sed command is:

$sed-- options [optional SCRIPT] [INPUT FILE or STREAM] find what you want to edit

In a visual editor, you usually don't need to think too much to find what you want to modify in a text file. Your eye (or screen reader) will scan the text to find the word you want to change or where you want to insert or delete the text, and then you can start typing. Sed has no interactive mode, so you need to tell it what conditions it must meet in order to run specific commands.

In these examples, suppose a file named example.txt contains the following text:

HelloworldThis is line three.Here is the final line. Line number

The specified line number tells sed to operate on only that line in the file.

For example, the following command selects line 1 of the file and prints it. Because the default action of sed after processing is also to print a line to standard output, the effect of this is to repeat the first line:

$sed'1p 'example.txthellohelloworldThis is line three.Here is the final line.

You can also specify the line number step by step. For example, 1 to 2 means to select a row for every two rows ("select a row for every two rows starting from the first row"). Directive 1: 3 means that starting from the first line, select one line for every three lines:

$sed'1p 'example.txthellohelloworldThis is line three.Here is the final line.Here is the final line. Row positioning

You can manipulate only the last line of the file by using $as the selector:

$sed'$p 'example.txthelloworldThis is line three.Here is the final line.Here is the final line.

In GNU sed, you can select multiple lines (for example, the first and last lines are printed by sed'1 recording p').

Reverse

You can reverse any number or position with the exclamation point (!) character. The following will select all rows except the first line:

$sed '1roomp' example.txthelloworldworldThis is line three.This is line three.Here is the final line.Here is the final line. Pattern matching

You can think of pattern matching as a search operation in a word processor or browser. You provide a word (a pattern) and then select the result. The syntax for pattern matching is / pattern/:

$sed'/ hello/p' example.txthellohelloworldThis is line three.Here is the final line.$ sed'/ line/p' example.txthelloworldThis is line three.This is line three.Here is the final line.Here is the final line. Edit with Sed

Once you have found the content you want to edit, you can do whatever you want. You can use the commands in sed to perform edits. The command in sed is not the sed command itself. If this is helpful, think of them as "actions" or "verbs" or "instructions".

The command in sed is a single letter, such as the p of the print command used in the previous example. They may be hard to remember at first, but like everything, you will learn about them as you practice.

P stands for printing

The p instruction prints anything in the current mode space.

D is used to delete

The d instruction deletes the mode space:

$sed'$d' example.txthelloworldThis is line three.$ sed '1d' example.txtworldThis is line three.Here is the final line.s for search and replacement

The s command searches for a pattern and replaces it with something else. This is probably the most popular and casual use of sed, and it is usually the first (and sometimes only) sed command that users learn. It is almost certainly the most useful command in text editing:

$sed's cosmopolitan opensource.com example.txthelloopensource.comThis is line three.Here is the final line.

You can also use some special features in your replacement text. For example,\ L converts alternate text to lowercase, and\ l converts only the next character. There are other features listed in the sed documentation (you can view them with the info sed command).

The special character in the replacement clause & refers to the matching pattern:

$sed 's/is/\ Ubunza' example.txthelloworldThIS is line three.Here IS the final line.

You can also use special logos to influence how s handles what it finds. The g flag tells s to replace all matches found on the line, not just the first match:

$sed 's/is/\ Ubland example.txthelloworldThIS IS line three.Here IS the final line.

Other important signs include the use of a number to indicate which matching pattern you want to affect:

$sed 's/is/\ Used example.txthelloworldThis IS line three.Here is the final line 2'.

The w flag, followed by a file name, will write matching lines to the file only if there is a change:

$sed 's/is/\ Ubuntu sed.log' example.txthelloworldThIS is line three.Here IS the final line.$ cat sed.logThIS is line three.Here IS the final line.

Flags can be combined:

$sed 's/is/\ Ubunza 2w sed.log' example.txthelloworldThis IS line three.Here is the final line.$ cat sed.logThis IS line three. Script

There are a lot of great sites that have sed "one-line scripts" that provide you with task-oriented sed commands to solve common problems. However, learning sed by yourself allows you to write your own one-line scripts, and these one-line scripts can be customized to your specific needs.

The script for sed can be written as a line in the terminal, or it can be saved to a file and executed using sed itself. I tend to write small notebooks as a command because I find that I rarely repeat sed commands in real life. When I write a sed script, it's usually for a file. For example, after writing the first draft of this article, I used sed to regulate the case of "sed", a task I may never do again.

You can issue a series of different commands to sed, separated by a semicolon (;).

$sed'3t; s/line/\ Using Grease 'example.txthelloworldThis is LINE three.This is the final line. Parenthesized range change

You can also limit which results are affected with curly braces ({}). When you enclose sed commands in curly braces, they only apply to specific selections. For example, the word "line" appears in two lines of sample text. You can force sed to affect only the last line by declaring the required matching criteria ($for the last line) and placing the s command you want to execute in parentheses immediately following it:

$sed'${s/line/ /} 'example.txthelloworldThis is line three.This is the final LINE. Thank you for reading! This is the end of this article on "how to use Sed text Editor in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out 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