Your Own Linux
Sed is a stream editor that lets you edit text files in an interactive way. It is an important command for the Linux and Unix operating systems, the MS-DOS operating system, the OS X operating system, and other free or open source software.
If you want to know all of its ins and outs, check out this tutorial!
It’s time to master sed. If you know sed, you’ll be able to edit files on Linux, Unix, and Mac OS X operating systems.
For example, you could use sed to edit a file with lots of multimedia audio and video files. You could also use it to apply simple fixes like removing all duplicate lines in a large text file. Or perhaps you want to write your own parsing tool for text files.
Let’s find out how to get the most out of sed.
What Is Sed?
The sed command is a stream editor. That means that it can be used to do basic text editing without using an editor program like Emacs or Vim. The name “sed” is short for “stream editor” or “standard editor”. It comes from the ed editor, which was the standard text-editor in Unix at the time it was created.
sed is a non-interactive command – it only reads its input and writes to its output. It can process text on the command line or from a file.
You can use sed to process text files: You can, for example, print all lines that match a certain pattern in a file. Or you might want to count how many times some pattern occurs in the file. Sed can also perform basic search and replace operations on text files as well as cut out parts of the input stream and replace them with other lines. One of the most powerful features of sed is its ability to transform text. It can, for example, take data in one format and convert it into a different human readable format.
How to Use Sed
You can use sed either on the command line or from within a script. Let’s see how you can use sed from the command line.
In this example, I want to print out all lines that contain the string “sample” in the datafile file1.txt:
$ # Edit data file. $ sed ‘s/sample/SUBJECT/g’ file1.txt # Print lines that contain “sample” $ sed ‘s/sample/Subject/g’ file1.txt
The example above should work in any Linux-based environment that supports the sed command. It should also work on Mac OS X. However, sed is not included in the default installation of Mac OS X. On Mac OS X, you’ll need to install the “xargs” command. This command is commonly found in package managers such as Homebrew or MacPorts. To install it from Homebrew, use this command:
$ brew install xargs
In addition to the Linux-based operating systems shown above, sed is included in the default installation of Solaris, FreeBSD and OpenBSD. As with Mac OS X, you need to install xargs if you plan on using sed in any of these environments. The following example shows an example of how you can use sed in FreeBSD:
$ # Edit data file $ sed ‘s/sample/Subject/g’ file1.txt # Print lines that contain “sample” $ sed ‘s/sample/Subject/g’ file1.txt
Conclusion
Congratulations! You’ve made it to the end of your sed tutorial. Now you should have a good amount of background information about the sed command and how to use it. One important thing you should take from this tutorial is how powerful sed can be when used correctly. It can, for example, do things that would otherwise require a lot of programming work.
Leave a Reply