search
HomeComputer TutorialsComputer KnowledgeStream Editor (SED): Basics

Stream Editor (SED): Basics

SED, also known as the Stream Editor, is a very useful tool. It is used to search for a specific word or pattern and then perform some operation on that word or pattern, or in other words, transform it. In Windows, SED is also known as the Find and Replace function. SED comes natively with Ubuntu, so there's nothing to install; just start using it. In this tutorial we will show you how to use SED or Stream Editor.

"S" Command

The most important command in SED or stream editor is the "s" command. The "s" stands for substitute. The syntax is as follows:

/regexp/replacement/flags

So, let's use a file called "file.txt" as an example. Here's what "file.txt" looks like if you trace it:


Let us use an example to illustrate how the "S" command works:

SED‘S/First/Moon/I’file.txt>moon.txt

When an expression like this is given, it means:

    • S—It stands for Substitute.
    • first—The word to search for in a file named "file.txt".
    • "Moon" - "First" is replaced by "Moon".
    • stands for ignore. Let’s ignore this part first.
    • File.txt - The file in which SED will search for patterns or words. In this case, the word "First" would be:
    • Searched in file .txt

    • Moon.txt – When the word “first” is replaced by “Moon”, it will be saved under “moon.txt”.

In SED, the word "first" is replaced with "moon" only in the first instance (this means that if "first" appears multiple times, only the first time will be replaced). This command searches in a file named "file.txt" and once the replacement is complete, the results will be saved to the "moon.txt" file.

This is what it looks like:


Please remember to put "/" where needed. If "/" is omitted, SED will not accept the command.

So far, we have only replaced "first time" with the word "encounter". Now, let's say we want to replace the word "line" (which appears multiple times - four times, to be specific) in the third line with the word "Angel".

How do we specifically target the third line? We use the following command:

SED‘3S/Line/Angel/I’file.txt>angel.txt

So, what just happened here? Okay, "3" specifies the line number. So it comes to the third line. Then, replace "angel.txt" with "line" in the file named "file.txt" and save the converted file as "angel.txt".


What if we want to replace or convert rows "3" and "4"?

Sed‘3,4s/line/Angel/I’file.txt>angel2.txt


Note that in the previous example, we used the "i" flag to represent ignore. Now, we use the "g" flag to represent global.

Let us use an example to illustrate how the "s" command works:

sed’s/line/sun/g ‘file.txt> sun. txt

When an expression like this is given, it means:

G stands for global. Remember, in the first example, when we used the "i" flag, there was only one substitution. Since we added a "g" to global, this means there are alternatives everywhere. So, instead of saying first row, second row, third row and last row, it says first sun, second sun, third sun and last sun. It replaces lines of words (everywhere) in the entire file with the word "sun".


Now, what if we want to select a line based on the words it contains? Well, we can see that the last line of "file.txt" has the word "last". Now, let's say we want "This is the last line". This is the last word." "This is the last ghost. This is the last sentence."

We write as follows:

Sed‘/last/S/line/host/’file.txt>.ghost.txt

The "last" here tells SED to find the line containing the word "last" and then replace the word "line" with "ghost" within that line.


Now, let's say we want to do the opposite. Suppose we want every line without the word "last" to have the word "line" changed to "host". Let's write the following:

Please select ‘/last/! S/line/ghost/’file.txt>.ghost 2.txt

As you can see here, every line except the last line (which contains the word "last") has the word "ghost" replaced with the word "line".


We can also use line numbers to achieve this:

Sed‘3,4! S/行/夜/I’file.txt>night.txt

In this case, lines 3 and 4 are omitted, but every other line has the word "line" replaced by the word "night".

Multiple commands

So, what if you have multiple commands? Would you rather do them one at a time or all at once to save yourself time and work?

What should we do if we want to change the word "first" to "day", the word "second" to "night", and the word "rest" to "ghost"? We do this using semicolons. Don't forget to put the semicolon at the end!

Please note that you do not have to put the "i" flag or the "ignore" flag, but you must put the slash (/) after the conversion phrase.

Now, let us verify it with an example:

sed’s/first/day/;s/second/night/;s/third/ghost/;s/last/ghost/;’file.txt> combination.txt

in conclusion

The Stream Editor, or SED, is a method of selecting words or patterns and converting them. It is effectively the command line equivalent of Windows' "find" and "replace" functions. SED commands can get very complex, but if you at least know the basics, you're ready! SED is actually a very powerful tool. While we can't cover them all in one tutorial, we cover the basics of SED. Essentially, we learned how to convert a specific word using the "s" command, where "s" stands for replacement word. We can replace words with other words, selectively select a line to replace, or even negate it. Either way, this is the easiest part about SED.

Happy coding!

The above is the detailed content of Stream Editor (SED): Basics. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
[Fixed] You Need to Authenticate Microsoft Services in Minecraft? - MiniTool[Fixed] You Need to Authenticate Microsoft Services in Minecraft? - MiniToolApr 27, 2025 am 12:51 AM

In this post, php.cn provides several effective solutions to the “you need to authenticate to Microsoft services” issue in Minecraft on the PC. If you get stuck in this issue, you should pay attention to this post.

Top 4 Fixes for High Power Usage in Task ManagerTop 4 Fixes for High Power Usage in Task ManagerApr 27, 2025 am 12:49 AM

In Task Manager, you can view how much power the processes on your computer use. What if you receive high power usage in Task Manager constantly?  In this post from php.cn Website, we will explore how to address this issue easily and effectively.

2 Windows Folders in C Drive: How to Deal with Them?2 Windows Folders in C Drive: How to Deal with Them?Apr 27, 2025 am 12:48 AM

The Windows folder contains the Windows operating system and is a crucial folder in Windows computers. Sometimes, you may see 2 Windows folders in the C drive. How to delete one of them? This post from php.cn tells you how to do that.

Top 4 Halo Infinite Trackers to Track KD, Stats, Ranks, and More! - MiniToolTop 4 Halo Infinite Trackers to Track KD, Stats, Ranks, and More! - MiniToolApr 27, 2025 am 12:47 AM

Many people are looking for a feasible Halo Infinite tracker. In this post, php.cn introduces several available Halo Infinite trackers to help you track the Halo Infinite KD, ranks, stats, etc. Now, keep on your reading.

10 Best Google Sheets Add-ons You Must Have - MiniTool10 Best Google Sheets Add-ons You Must Have - MiniToolApr 27, 2025 am 12:46 AM

Have you ever heard of Google Sheets add-ons? Do you have any idea how to install them in your Google Sheets? Do you want to get the best and the free Google Sheets add-ons? This article on php.cn focuses on these questions and provides you with seve

Enable/Disable Ctrl Alt Delete Secure Sign-In on Windows 11 - MiniToolEnable/Disable Ctrl Alt Delete Secure Sign-In on Windows 11 - MiniToolApr 27, 2025 am 12:45 AM

Secure Sign-In is a handy way to keep your PC safe, but if you don't want it around, here's how to disable it for Windows 11. This post from php.cn provides 3 ways for you to enable or disable Ctrl Alt Delete secure sign-in on Windows 11.

How to Fix Word Dictation Not Working Issue in Windows 10 - MiniToolHow to Fix Word Dictation Not Working Issue in Windows 10 - MiniToolApr 27, 2025 am 12:44 AM

Have you ever heard of Microsoft Word dictation? What if Word dictation not working? This post from php.cn shows you the basic information about Word dictation and some feasible ways to fix the “Word dictation not working” issue.

What Is Atlas VPN? How to Free Download Atlas VPN for Use? - MiniToolWhat Is Atlas VPN? How to Free Download Atlas VPN for Use? - MiniToolApr 27, 2025 am 12:43 AM

What is Atlas VPN used for? Is Atlas VPN safe? Can you use Atlas VPN for free? How to download Atlas VPN & install it on your device for use? Refer to this post from php.cn and you can find details about this VPN service and Atlas VPN download fo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software