Your Own Linux
If you find yourself in a situation where you want to append an entire file, the syntax of sed is:
$ sed ‘1i\G’ filename.txt
To append a single line to the end of a file:
$ echo “A new line” >> filename.txt
Above are some basic examples on how to use sed. So what is the sed command?
The sed command stands for “stream editor”. Sed reads and writes lines of text. It comes in two major forms: the interactive version, and the non-interactive version. The basic syntax of sed is as follows:
$sed [command_options] filename
To use sed in an interactive context, you can simply run it without arguments to get a help page. To get a list of commands available, run $sed -h. To get help on a specific command, run $sed -?.
The ‘n’ command instructs sed to print out each line of the file and print it to the screen. This is not suitable for editing files. Instead, use ‘r’ or ‘w’ to append the new data after each line or just write them verbatim to a file.
$sed ‘1i\G’ filename.txt
$sed -r filename.txt
So whether you are a student, a teacher, or a working professional, sed can be really useful.
Another useful tool to use with sed is grep.
Grep is used to search files for lines that match a given pattern. It allows you to print out only those lines of the file that match your pattern, which is helpful in preparing your files for editing with sed. Grep is a very versatile tool, and you can use it to search for patterns in files. You can also use it to filter data in real-time (for example, as you type search terms into the terminal). This way, if you see that a command outputs too much information, you can view only those lines that begin with your search term.
$grep “pattern” filename.txt
To learn more about grep and its various usages, run $man grep or visit http://www.computerhope.com/web/.
Sed vs. awk: one is not as good as the other …
If you want to use sed and awk to do the same thing, you are in for a disappointment. Good luck! (Although, with computers becoming more powerful these days, it may be possible.)
Sed is useful for plain text editing only. If you want to print out a report or manipulate data in another format (such as HTML), then awk is likely the toolset of choice. Awk is a separate program from sed, and it uses its own syntax.
Here’s the difference between awk and sed:
$ awk “BEGIN {print \”Hello World!\”}”. Hello World!
$sed “s/World/SiliconValley/g” /etc/world
As you can see, when you want to print out data in one command, awk is a better choice than sed. However, when you want to do something with the strings, sed is better (let’s say changing one character in the string).
Awk can be used to print text, but it cannot be used to search and replace. For example, if you use the awk command on /etc/passwd and press Control-D while searching for “world”, awk will print out “siliconvalley” without error. But if you also search for “world”, awk prints no data at all.
Conclusion
I hope you enjoyed this article. Now you can use sed to append lines to your file and search for patterns.
Leave a Reply