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
如何解决 Windows 11 中的文件名或扩展名过长的问题?如何解决 Windows 11 中的文件名或扩展名过长的问题?Apr 22, 2023 pm 04:37 PM

您在传输文件时是否遇到过任何问题,并且禁止您这样做?好吧,许多Windows用户最近报告说,他们在将文件复制并粘贴到文件夹中时遇到了问题,其中抛出了一个错误,提示“目标文件夹的文件名太长”。此外,其他一些Windows用户在打开任何文件时表示失望,并说“文件名或扩展名太长”,他们无法打开文件。这不允许他们将文件传输到任何其他文件夹,这让用户感到失望。在分析问题时,我们提出了一系列解决方案,可能有助于缓解问题,用户可以轻松传输文件。如果您也遇到类似情况,请参阅此帖子以了解更多信息。来源:https

如何在Windows 11、10中关闭Windows Defender智能屏幕?如何在Windows 11、10中关闭Windows Defender智能屏幕?Apr 26, 2023 am 11:46 AM

许多Windows用户最近报告说,当WindowsDefenderSmartScreen警告用户不要启动MicrosoftWindows无法识别的应用程序时,他们感到恼火,他们每次都必须单击“仍然运行”选项。Windows用户不确定他们目前可以做些什么来避免或禁用它。在研究了这个问题后,我们发现系统上的WindowsDefender功能可以通过设置应用程序或本地组策略编辑器或通过调整注册表文件来禁用。通过这样做,用户将不再需要面对防守者SmartScreen。如果您的系统也遇到

15 款 Python 编辑器/ IDE 详细攻略,总有一款适合你!15 款 Python 编辑器/ IDE 详细攻略,总有一款适合你!Aug 09, 2023 pm 05:44 PM

写 Python 代码最好的方式莫过于使用集成开发环境(IDE)了。它们不仅能使你的工作更加简单、更具逻辑性,还能够提升编程体验和效率。每个人都知道这一点。而问题在于,如何从众多选项中选择最好的 Python 开发环境。

修复 Windows 11/10 登录选项被禁用的问题修复 Windows 11/10 登录选项被禁用的问题May 07, 2023 pm 01:10 PM

许多Windows用户都遇到过由于登录尝试失败或多次关闭系统而无法登录Windows11/10系统的问题。用户很沮丧,因为他们对此无能为力。用户可能忘记了登录系统的PIN码,或者在使用或安装软件时出现卡顿,系统可能被多次强制关闭。因此,我们制定了一份最好的可用解决方案列表,这些解决方案无疑将帮助消费者解决这个问题。要了解更多信息,请继续阅读本文。注意:在此之前,请确保您拥有系统的管理员凭据和Microsoft帐户密码以重置PIN。如果没有,请等待一个小时左右,然后尝试使用正确的PIN

强烈推荐十个 Python IDE 和代码编辑器!强烈推荐十个 Python IDE 和代码编辑器!Apr 19, 2023 pm 07:04 PM

Python非常易学,强大的编程语言。Python包括高效高级的数据结构,提供简单且高效的面向对象编程。Python的学习过程少不了IDE或者代码编辑器,或者集成的开发编辑器(IDE)。这些Python开发工具帮助开发者加快使用Python开发的速度,提高效率。高效的代码编辑器或者IDE应该会提供插件,工具等能帮助开发者高效开发的特性。1.VimVim可以说是Python最好的IDE。Vim是高级文本编辑器,旨在提供实际的Unix编辑器‘Vi’功能,支持更多更完善的特

如何使用 ClipChamp:免费的 Windows 11 视频编辑器如何使用 ClipChamp:免费的 Windows 11 视频编辑器Apr 20, 2023 am 11:55 AM

还记得Windows7上的WindowsMovieMaker吗?自从停止WindowsMovieMaker以来,微软还没有推出任何真正的电影制作者。另一方面,他们尝试用一个小巧轻便的内置视频编辑器来改造照片应用程序。很长一段时间后,微软推出了Clipchamp,这是一款适用于所有Windows11设备的更好的视频处理器。在本文中,我们将深入探讨如何从Windows11设备上的Clipchamp应用程序中获取所有内容。如何使用Clipchamp–详细教程我们提供

Golang编辑器推荐:五个适合开发的选择Golang编辑器推荐:五个适合开发的选择Jan 19, 2024 am 09:00 AM

随着Golang的流行和普及,越来越多的开发者开始使用这门编程语言。然而,和其他流行的编程语言一样,Golang的开发需要选择一款适合的编辑器来提高开发效率。在本文中,我们将介绍五个适合Golang开发的编辑器。VisualStudioCodeVisualStudioCode(简称VSCode)是微软开发的一款免费的跨平台编辑器。它是基于Elect

如何在 Windows 11 和 10 上使用 Clipchamp 视频编辑器如何在 Windows 11 和 10 上使用 Clipchamp 视频编辑器Apr 17, 2023 pm 07:55 PM

如何在Windows上安装和使用ClipchampClipchamp应用程序尚未预装在Windows上,但这是未来的计划。同时,您需要先下载并安装Clipchamp。要在Windows11和Windows10上安装和使用Clipchamp:从MicrosoftStore下载并安装Clipchamp。安装后,在开始菜单中搜索Clipchamp以启动它。在Clipchamp窗口中,您需要使用您的Microsoft或Google帐户登录,或者使用您自己的个人电子邮

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment