Stream Editor – Useful Recipes ”; Previous Next SED is an amazing utility that allows multiple ways to solve a problem. This is the UNIX way and SED perfectly proves that. GNU/Linux provides many useful utilities to perform day-to-day tasks. Let us simulate a few utilities using SED. Sometimes it may appear we are solving an easy problem the hard way, but the purpose is just to demonstrate the power of SED. Cat Command In the following example, each line is printed as a part of the default workflow. [jerry]$ sed ”” books.txt On executing the above code, you get the following result: A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin The following example uses print command to display the file contents. [jerry]$ sed -n ”p” books.txt On executing the above code, you get the following result: A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin Removing Empty Lines In the following example, “^$” implies empty line, and empty lines are deleted when a pattern match succeeds. [jerry]$ echo -e “Line #1nnnLine #2″ | sed ”/^$/d” On executing the above code, you get the following result: Line #1 Line #2 Similarly, the following example prints the line only when it is non-empty. [jerry]$ echo -e “Line #1nnnLine #2″ | sed -n ”/^$/!p” On executing the above code, you get the following result: Line #1 Line #2 Removing Commented Lines from a C++ Program Let us create a sample C++ program. #include <iostream> using namespace std; int main(void) { // Displays message on stdout. cout >> “Hello, World !!!” >> endl; return 0; // Return success. } Now remove the comments using the following regular expression. [jerry]$ sed ”s|//.*||g” hello.cpp On executing the above code, you get the following result: #include <iostream> using namespace std; int main(void) { cout >> “Hello, World !!!” >> endl; return 0; } Adding Comments Before Certain Lines The following example adds comments before line numbers 3 to 5. [jerry]$ sed ”3,5 s/^/#/” hello.sh On executing the above code, you get the following result: #!/bin/bash #pwd #hostname #uname -a who who -r lsb_release -a Wc -l command The “wc -l” command counts the number of lines present in the file. The following SED expression simulates the same. [jerry]$ sed -n ”$ =” hello.sh On executing the above code, you get the following result: 8 Head Command By default, the head command prints the first 10 lines of the file. Let us simulate the same behavior with SED. [jerry]$ sed ”10 q” books.txt On executing the above code, you get the following result: A Storm of Swords George R. R. Martin The Two Towers J. R. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. R. Tolkien The Pilgrimage Paulo Coelho Tail -1 Command The “tail -1” prints the last line of the file. The following syntax shows its simulation. [jerry]$ echo -e “Line #1nLine #2″ > test.txt [jerry]$ cat test.txt On executing the above code, you get the following result: Line #1 Line #2 Let us write the SED script. [jerry]$ sed -n ”$p” test.txt On executing the above code, you get the following result: Line #2 Dos2unix Command In DOS environment, a newline is represented by a combination of CR/LF characters. The following simulation of “dos2unix” command converts a DOS newline character to UNIX newline character. In GNU/Linux, this character is often treated as “^M” (Control M) character. [jerry]$ echo -e “Line #1rnLine #2r” > test.txt [jerry]$ file test.txt On executing the above code, you get the following result: test.txt: ASCII text, with CRLF line terminators Let us simulate the command using SED. [jerry]$ sed ”s/^M$//” test.txt > new.txt # Press “ctrl+v” followed “ctrl+m” to generate “^M” character. [jerry]$ file new.txt On executing the above code, you get the following result: new.txt: ASCII text Now let us display the file contents. [jerry]$ cat -vte new.txt On executing the above code, you get the following result: Line #1$ Line #2$ Unix2dos command Similar to “dos2unix”, there is “unix2dos” command which converts UNIX newline character to DOS newline character. The following example shows simulation of the same. [jerry]$ echo -e “Line #1nLine #2″ > test.txt [jerry]$ file test.txt On executing the above code, you get the following result: test.txt: ASCII text Let us simulate the command using SED. [jerry]$ sed ”s/$/r/” test.txt > new.txt [jerry]$ file new.txt On executing the above code, you get the following result: new.txt: ASCII text, with CRLF line terminators Now let us display the file contents. Now let us display the file contents. On executing the above code, you get the following result: Line #1^M$ Line #2^M$ Cat -E command The “cat -E” command shows the end of line by Dollar($) character. The following SED example is simulation of the same. [jerry]$ echo -e “Line #1nLine #2″ > test.txt [jerry]$ cat -E test.txt On executing the above code, you get the following result: Line #1$ Line #2$ Let us simulate the command using SED. [jerry]$ sed ”s|$|&$|” test.txt On executing the above code, you get the following result: Line #1$ Line #2$ Cat -ET Command The “cat -ET” command shows the Dollar($) symbol at the end of each line and displays the TAB characters as “^I”. The following example shows the simulation of “cat -ET” command using SED. [jerry]$ echo -e “Line #1tLine #2″ > test.txt [jerry]$ cat -ET test.txt On executing the above code, you get the following result: Line #1^ILine #2$ Let us simulate the command using SED. [jerry]$ sed -n ”l” test.txt | sed ”y/\t/^I/” On executing the above code, you get the following result: Line #1^ILine #2$
Category: sed
Sed – Pattern Range
Stream Editor – Pattern Range ”; Previous Next In the previous chapter, we learnt how SED handles an address range. This chapter covers how SED takes care of a pattern range. A pattern range can be a simple text or a complex regular expression. Let us take an example. The following example prints all the books of the author Paulo Coelho. [jerry]$ sed -n ”/Paulo/ p” books.txt On executing the above code, you get the following result: 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288 In the above example, the SED operates on each line and prints only those lines that match the string Paulo. We can also combine a pattern range with an address range. The following example prints lines starting with the first match of Alchemist until the fifth line. [jerry]$ sed -n ”/Alchemist/, 5 p” books.txt On executing the above code, you get the following result: 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 We can use the Dollar($) character to print all the lines after finding the first occurrence of the pattern. The following example finds the first occurrence of the pattern The and immediately prints the remaining lines from the file [jerry]$ sed -n ”/The/,$ p” books.txt On executing the above code, you get the following result: 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 We can also specify more than one pattern ranges using the comma(,) operator. The following example prints all the lines that exist between the patterns Two and Pilgrimage. [jerry]$ sed -n ”/Two/, /Pilgrimage/ p” books.txt On executing the above code, you get the following result: 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 Additionally, we can use the plus(+) operator within a pattern range. The following example finds the first occurrence of the pattern Two and prints the next 4 lines after that. [jerry]$ sed -n ”/Two/, +4 p” books.txt On executing the above code, you get the following result: 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 We have supplied here only a few examples to get you acquainted with SED. You can always get to know more by trying a few examples on your own. Print Page Previous Next Advertisements ”;
Sed – Branches
Stream Editor – Branches ”; Previous Next Branches can be created using the t command. The t command jumps to the label only if the previous substitute command was successful. Let us take the same example as in the previous chapter, but instead of printing a single hyphen(-), now we print four hyphens. The following example illustrates the usage of the t command. [jerry]$ sed -n ” h;n;H;x s/n/, / :Loop /Paulo/s/^/-/ /—-/!t Loop p” books.txt When the above code is executed, it will produce the following result. A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien —-The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien —-The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin In the above example, the first two commands are self-explanatory. The third command defines a label Loop. The fourth command prepends hyphen(-) if the line contains the string “Paulo” and the t command repeats the procedure until there are four hyphens at the beginning of the line. To improve readability, each SED command is written on a separate line. Otherwise, we can write a one-liner SED as follows: [jerry]$ sed -n ”h;n;H;x; s/n/, /; :Loop;/Paulo/s/^/-/; /—-/!t Loop; p” books.txt When the above code is executed, it will produce the following result. A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien —-The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien —-The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin Print Page Previous Next Advertisements ”;
Sed – Discussion
Discuss Sed ”; Previous Next This tutorial takes you through all about Stream EDitor (Sed), one of the most prominent text-processing utilities on GNU/Linux. Similar to many other GNU/Linux utilities, it is stream-oriented and uses simple programming language. It is capable of solving complex text processing tasks with few lines of code. This easy, yet powerful utility makes GNU/Linux more interesting. Print Page Previous Next Advertisements ”;
Sed – Quick Guide
Stream Editor – Quick Guide ”; Previous Next Stream Editor – Overview The acronym SED stands for Stream EDitor. It is a simple yet powerful utility that parses the text and transforms it seamlessly. SED was developed during 1973–74 by Lee E. McMahon of Bell Labs. Today, it runs on all major operating systems. McMahon wrote a general-purpose line-oriented editor, which eventually became SED. SED borrowed syntax and many useful features from ed editor. Since its beginning, it has support for regular expressions. SED accepts inputs from files as well as pipes. Additionally, it can also accept inputs from standard input streams. SED is written and maintained by the Free Software Foundation (FSF) and it is distributed by GNU/Linux. Hence it is often referred to as GNU SED. To a novice user, the syntax of SED may look cryptic. However, once you get familiar with its syntax, you can solve many complex tasks with a few lines of SED script. This is the beauty of SED. Typical Uses of SED SED can be used in many different ways, such as: Text substitution, Selective printing of text files, In-a-place editing of text files, Non-interactive editing of text files, and many more. Stream Editor – Environment This chapter describes how to set up the SED environment on your GNU/Linux system. Installation Using Package Manager Generally, SED is available by default on most GNU/Linux distributions. Use which command to identify whether it is present on your system or not. If not, then install SED on Debian based GNU/Linux using apt package manager as follows: [jerry]$ sudo apt-get install sed After installation, ensure that SED is accessible via command line. [jerry]$ sed –versio On executing the above code, you get the following result: sed (GNU sed) 4.2.2 Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Jay Fenlason, Tom Lord, Ken Pizzini, and Paolo Bonzini. GNU sed home page: . General help using GNU software: . E-mail bug reports to: . Be sure to include the word “sed” somewhere in the “Subject:” field. Similarly, to install SED on RPM based GNU/Linux, use yum package manager as follows: [root]# yum -y install sed After installation, ensure that SED is accessible via command line. [root]# sed –version On executing the above code, you get the following result: GNU sed version 4.2.1 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law. GNU sed home page: . General help using GNU software: . E-mail bug reports to: . Be sure to include the word “sed” somewhere in the “Subject:” field. Installation from Source Code As GNU SED is a part of the GNU project, its source code is available for free download. We have already seen how to install SED using package manager. Let us now understand how to install SED from its source code. The following installation is applicable to any GNU/Linux software, and for most other freely-available programs as well. Here are the installation steps: Download the source code from an authentic place. The command-line utility wget serves this purpose. [jerry]$ wget ftp://ftp.gnu.org/gnu/sed/sed-4.2.2.tar.bz2 Decompress and extract the downloaded source code. [jerry]$ tar xvf sed-4.2.2.tar.bz2 Change into the directory and run configure. [jerry]$ ./configure Upon successful completion, the configure generates Makefile. To compile the source code, issue a make command. [jerry]$ make You can run the test suite to ensure the build is clean. This is an optional step. [jerry]$ make check Finally, install the SED utility. Make sure you have superuser privileges. [jerry]$ sudo make install That is it! You have successfully compiled and installed SED. Verify it by executing the sed command as follows: [jerry]$ sed –version On executing the above code, you get the following result: sed (GNU sed) 4.2.2 Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Jay Fenlason, Tom Lord, Ken Pizzini, and Paolo Bonzini. GNU sed home page: . General help using GNU software: . E-mail bug reports to: . Be sure to include the word “sed” somewhere in the “Subject:” field. Stream Editor – Workflow In this chapter, we will explore how SED exactly works. To become an expert SED user, one needs to know its internals. SED follows a simple workflow: Read, Execute, and Display. The following diagram depicts the workflow. Read: SED reads a line from the input stream (file, pipe, or stdin) and stores it in its internal buffer called pattern buffer. Execute: All SED commands are applied sequentially on the pattern buffer. By default, SED commands are applied on all lines (globally) unless line addressing is specified. Display: Send the (modified) contents to the output stream. After sending the data, the pattern buffer will be empty. The above process repeats until the file is exhausted. Points to Note Pattern buffer is a private, in-memory, volatile storage area used by the SED. By default, all SED commands are applied on the pattern buffer, hence the input file remains unchanged. GNU SED provides a way to modify the input file in-a-place. We will explore about it in later sections. There is another memory area called hold buffer which is also private, in- memory, volatile storage area. Data can be stored in a hold buffer for later retrieval. At the end of each cycle, SED removes the contents of the pattern buffer but the contents of the hold buffer remains persistent between SED cycles. However SED commands cannot be directly executed on hold buffer, hence SED allows data movement between the hold buffer and
Sed – Special Characters
Stream Editor – Special Characters ”; Previous Next SED provides two special characters which are treated as commands. This chapter illustrates the usage of these two special characters. = Command The “=” command deals with line numbers. Given below is the syntax of the “=” command: [/pattern/]= [address1[,address2]]= The = command writes the line number followed by its contents on the standard output stream. The following example illustrates this. [jerry]$ sed ”=” books.txt On executing the above code, you get the following result: 1 1) A Storm of Swords, George R. R. Martin, 1216 2 2) The Two Towers, J. R. R. Tolkien, 352 3 3) The Alchemist, Paulo Coelho, 197 4 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5 5) The Pilgrimage, Paulo Coelho, 288 6 6) A Game of Thrones, George R. R. Martin, 864 Let us print the line numbers and the contents of the first four lines. The following command prints the first four lines with line numbers and the remaining without line numbers. [jerry]$ sed ”1, 4=” books.txt On executing the above code, you get the following result: 1 1) A Storm of Swords, George R. R. Martin, 1216 2 2) The Two Towers, J. R. R. Tolkien, 352 3 3) The Alchemist, Paulo Coelho, 197 4 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 Additionally, we can instruct the SED to print line numbers when a pattern match succeeds. The following example prints the line number that contains the pattern “Paulo”. [jerry]$ sed ”/Paulo/ =” books.txt On executing the above code, you get the following result: 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 Can you guess what the following SED command does? [jerry]$ sed -n ”$ =” books.txt On executing the above code, you get the following result: 6 Yes, you are right. It counts the total number of lines present in the file. Let us demystify the code. In the command section, we used “$ =” which prints the line number of the last line followed by its contents. But we also provided the -n flag which suppresses the default printing of the pattern buffer. Hence, only the last line number is displayed. & Command SED supports the special character &. Whenever a pattern match succeeds, this special character stores the matched pattern. It is often used with the substitution command. Let us see how we can leverage this efficient feature. Each line in the book.txt file is numbered. Let us add the words Book number at the beginning of each line. The following example illustrates this. [jerry]$ sed ”s/[[:digit:]]/Book number &/” books.txt On executing the above code, you get the following result: Book number 1) A Storm of Swords, George R. R. Martin, 1216 Book number 2) The Two Towers, J. R. R. Tolkien, 352 Book number 3) The Alchemist, Paulo Coelho, 197 Book number 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 Book number 5) The Pilgrimage, Paulo Coelho, 288 Book number 6) A Game of Thrones, George R. R. Martin, 864 This example is very simple. First, we search for the first occurrence of a digit, which is the line number (that is why we used [[:digit:]]) and the SED automatically stores the matched pattern in the special character &. In the second step, we insert the words Book number before each matched pattern, i.e., before every line. Let us take another example. In the book.txt file, the last digit implies the number of pages of the book. Let us add “Pages =” before that. To do this, find the last occurrence of the digit and replace it with “Pages = &”. Here, & stores the matched pattern, i.e., the number of pages [jerry]$ sed ”s/[[:digit:]]*$/Pages = &/” books.txt On executing the above syntax, you get the following result: 1) A Storm of Swords, George R. R. Martin, Pages = 1216 2) The Two Towers, J. R. R. Tolkien, Pages = 352 3) The Alchemist, Paulo Coelho, Pages = 197 4) The Fellowship of the Ring, J. R. R. Tolkien, Pages = 432 5) The Pilgrimage, Paulo Coelho,Pages = 288 6) A Game of Thrones, George R. R. Martin, Pages = 864 For the time being, just remember that [[:digit:]]*$ finds the last occurrence of the digit. In the chapter “Regular Expressions, we will explore more about regular expressions. Print Page Previous Next Advertisements ”;
Sed – Managing Patterns
Stream Editor – Managing Patterns ”; Previous Next We have already discussed the use of pattern and hold buffer. In this chapter, we are going to explore more about their usage. Let us discuss the n command which prints the pattern space. It will be used in conjunction with other commands. Given below is the syntax of then command. [address1[,address2]]n Let us take an example. [jerry]$ sed ”n” books.txt When the above code is executed, it will produce the following result: 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 The n command prints the contents of the pattern buffer, clears the pattern buffer, fetches the next line into the pattern buffer, and applies commands on it. Let us consider there are three SED commands before n and two SED commands after n as follows: Sed command #1 Sed command #2 Sed command #3 n command Sed command #4 Sed command #5 In this case, SED applies the first three commands on the pattern buffer, clears the pattern buffer, fetches the next line into the pattern buffer, and thereafter applies the fourth and fifth commands on it. This is a very important concept. Do not go ahead without having a clear understanding of this. The hold buffer holds data, but SED commands cannot be applied directly on the hold buffer. Hence, we need to bring the hold buffer data into the pattern buffer. SED provides the x command to exchange the contents of pattern and hold buffers. The following commands illustrate the x command. Let us slightly modify the books.txt file. Say, the file contains book titles followed by their author names. After modification, the file should look like this: [jerry]$ cat books.txt On executing the above code, you get the following result: A Storm of Swords George R. R. Martin The Two Towers J. R. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. R. Tolkien The Pilgrimage Paulo Coelho A Game of Thrones George R. R. Martin Let us exchange the contents of the two buffers. For instance, the following example prints only the names of authors. [jerry]$ sed -n ”x;n;p” books.txt On executing the above code, you get the following result: George R. R. Martin J. R. R. Tolkien Paulo Coelho J. R. R. Tolkien Paulo Coelho George R. R. Martin Let us understand how this command works. Initially, SED reads the first line, i.e., A Storm of Swords into the pattern buffer. x command moves this line to the hold buffer. n fetches the next line, i.e., George R. R. Martin into the pattern buffer. The control passes to the command followed by n which prints the contents of the pattern buffer. The process repeats until the file is exhausted. Now let us exchange the contents of the buffers before printing. Guess, what happens? Yes, it prints the titles of books. [jerry]$ sed -n ”x;n;x;p” books.txt On executing the above code, you get the following result: A Storm of Swords The Two Towers The Alchemist The Fellowship of the Ring The Pilgrimage A Game of Thrones The h command deals with the hold buffer. It copies data from the pattern buffer to the hold buffer. Existing data from the hold buffer gets overwritten. Note that the h command does not move data, it only copies data. Hence, the copied data remains as it is in the pattern buffer. Given below is the syntax of the h command. [address1[,address2]]h The following command prints only the titles of the author Paulo Coelho. [jerry]$ sed -n ”/Paulo/!h; /Paulo/{x;p}” books.txt On executing the above code, you get the following result: The Alchemist The Pilgrimage Let us understand how the above command works. The contents of books.txt follow a specific format. The first line is the book title followed by the author of the book. In the above command, “!” is used to reverse the condition, i.e., line is copied to the hold buffer only when a pattern match does not succeed. And curly braces {} are used to group multiple SED commands In the first pass of the command, SED reads the first line, i.e., A Storm of Swords into the pattern buffer and checks whether it contains the pattern Paulo or not. As the pattern match does not succeed, it copies this line to the hold buffer. Now both the pattern buffer and the hold buffer contain the same line i.e., A Storm of Swords. In the second step, it checks whether the line contains the pattern Paulo or not. As the pattern does not match, it does not do anything. In second pass, it reads the next line George R. R. Martin into the pattern buffer and applies the same steps. For the next three lines, it does the same thing. At the end of the fifth pass, both the buffers contain The Alchemist. At the start of the sixth pass, it reads the line Paulo Coelho and as the pattern matches, it does not copy this line into the hold buffer. Hence, the pattern buffer contains Paulo Coelho, and the hold buffer contains The Alchemist. Thereafter, it checks whether the pattern buffer contains the pattern Paulo. As the pattern match succeeds, it exchanges the contents of the pattern buffer with the hold buffer. Now the pattern buffer contains The Alchemist and the hold buffer contains Paulo Coelho. Finally, it prints the contents of the pattern buffer. The same steps are applied to the pattern The Pilgrimage. The h command destroys the previous contents of the hold buffer. This is not always acceptable, as sometimes we need to preserve the contents. For this purpose, SED provides the H command which appends the contents to the hold buffer
Sed – Strings
Stream Editor – Strings ”; Previous Next Substitute Command Text substitution operations like “find and replace” are common in any text editor. In this section, we illustrate how SED performs text substitution. Given below is the syntax of the substitution command. [address1[,address2]]s/pattern/replacement/[flags] Here, address1 and address2 are the starting and ending addresses respectively, which can be either line numbers or pattern strings. Both these addresses are optional parameters. The pattern is the text which we want to replace with the replacement string. Additionally, we can specify optional flags with the SED. In the books.txt file, we have used comma(,) to separate each column. Let us use vertical bar(|) to separate each column. To do this, replace comma(,) with vertical bar(|). [jerry]$ sed ”s/,/ | /” books.txt On executing the above code, you get the following result: 1) A Storm of Swords | George R. R. Martin, 1216 2) The Two Towers | J. R. R. Tolkien, 352 3) The Alchemist | Paulo Coelho, 197 4) The Fellowship of the Ring | J. R. R. Tolkien, 432 5) The Pilgrimage | Paulo Coelho, 288 6) A Game of Thrones | George R. R. Martin, 864 If you observe carefully, only the first comma is replaced and the second remains as it is. Why? As soon as the pattern matches, SED replaces it with the replacement string and moves to the next line. By default, it replaces only the first occurrence. To replace all occurrences, use the global flag (g) with SED as follows: [jerry]$ sed ”s/,/ | /g” books.txt On executing the above code, you get the following result: 1) A Storm of Swords | George R. R. Martin | 1216 2) The Two Towers | J. R. R. Tolkien | 352 3) The Alchemist | Paulo Coelho | 197 4) The Fellowship of the Ring | J. R. R. Tolkien | 432 5) The Pilgrimage | Paulo Coelho | 288 6) A Game of Thrones | George R. R. Martin | 864 Now all occurrences of commas(,) are replaced with vertical bar(|). We can instruct the SED to perform text substitution only when a pattern match succeeds. The following example replaces comma(,) with vertical bar(|) only when a line contains the pattern The Pilgrimage. [jerry]$ sed ”/The Pilgrimage/ s/,/ | /g” books.txt On executing the above code, you get the following result: 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage | Paulo Coelho | 288 6) A Game of Thrones, George R. R. Martin, 864 In addition to this, SED can replace a specific occurrence of the pattern. Let us replace only the second instance of comma(,) with vertical bar(|). [jerry]$ sed ”s/,/ | /2” books.txt On executing the above code, you get the following result: 1) A Storm of Swords, George R. R. Martin | 1216 2) The Two Towers, J. R. R. Tolkien | 352 3) The Alchemist, Paulo Coelho | 197 4) The Fellowship of the Ring, J. R. R. Tolkien | 432 5) The Pilgrimage,Paulo Coelho | 288 6) A Game of Thrones, George R. R. Martin | 864 In the above example, the number at the end of the SED command (or at the place of flag) implies the 2nd occurrence. SED provides an interesting feature. After performing substitution, SED provides an option to show only the changed lines. For this purpose, SED uses the p flag which refers to print. The following example lists only changed lines. [jerry]$ sed -n ”s/Paulo Coelho/PAULO COELHO/p” books.txt On executing the above code, you get the following result: 3) The Alchemist, PAULO COELHO, 197 5) The Pilgrimage, PAULO COELHO, 288 We can store changed lines in another file as well. To achieve this result, use the w flag. The following example shows how to do it. [jerry]$ sed -n ”s/Paulo Coelho/PAULO COELHO/w junk.txt” books.txt We used the same SED command. Let us verify the contents of the junk.txt file. [jerry]$ cat junk.txt On executing the above code, you get the following result: 3) The Alchemist, PAULO COELHO, 197 5) The Pilgrimage, PAULO COELHO, 288 To perform case-insensitive substitution, use the i flag which implies ignore case. The following example performs case-insensitive substitution. [jerry]$ sed -n ”s/pAuLo CoElHo/PAULO COELHO/pi” books.txt On executing the above code, you get the following result: 3) The Alchemist, PAULO COELHO, 197 5) The Pilgrimage, PAULO COELHO, 288 So far, we have used only the foreslash(/) character as a delimiter, but we can also use vertical bar(|), at sign(@), caret(^), exclamation mark(!) as a delimiter. The following example shows how to use other characters as a delimiter. Let us assume you need to replace the path /bin/sed with /home/jerry/src/sed/sed-4.2.2/sed. Hence, your SED command looks like this: [jerry]$ echo “/bin/sed” | sed ”s//bin/sed//home/jerry/src/sed/sed-4.2.2/sed/” On executing the above code, you get the following result: /home/jerry/src/sed/sed-4.2.2/sed We can make this command more readable and easy to understand. Let us use vertical bar(|) as delimiter and see the result. [jerry]$ echo “/bin/sed” | sed ”s|/bin/sed|/home/jerry/src/sed/sed-4.2.2/sed|” On executing the above code, you get the following result: /home/jerry/src/sed/sed-4.2.2/sed Indeed! We got the same result and the syntax is more readable. Similarly, we can use the “at” sign (@) as a delimiter as follows: [jerry]$ echo “/bin/sed” | sed ”s@/bin/sed@/home/jerry/src/sed/sed-4.2.2/sed@” On executing the above code, you get the following result: /home/jerry/src/sed/sed-4.2.2/sed In addition to this, we can use caret(^) as a delimiter. [jerry]$ echo “/bin/sed” | sed ”s^/bin/sed^/home/jerry/src/sed/sed-4.2.2/sed^” On executing the above code, you get the following result: /home/jerry/src/sed/sed-4.2.2/sed We can also use exclamation mark (!) as a delimiter as follows: [jerry]$ echo “/bin/sed” | sed ”s!/bin/sed!/home/jerry/src/sed/sed-4.2.2/sed!” On executing the above code, you get the following result: /home/jerry/src/sed/sed-4.2.2/sed Generally, backslash(/) is used as a delimiter but sometimes it is more convenient to use other supported delimiters with SED. Creating a Substring We learnt the powerful substitute command.
Sed – Basic Commands
Stream Editor – Basic Commands ”; Previous Next This chapter describes several useful SED commands. Delete Command SED provides various commands to manipulate text. Let us first explore about the delete command. Here is how you execute a delete command: [address1[,address2]]d address1 and address2 are the starting and the ending addresses respectively, which can be either line numbers or pattern strings. Both of these addresses are optional parameters. As the name suggests, the delete command is used to perform delete operation and since the SED operates on line, we can say that this command is used to delete lines. Note that the delete command removes lines only from the pattern buffer; the line is not sent to the output stream and the original file remains unchanged. The following example illustrates the point. [jerry]$ sed ”d” books.txt But where is the output? If no line address is provided, then the SED operates on every line by default. Hence, it deletes all the lines from the pattern buffer. That is why the command does not print anything on the standard output. Let us instruct the SED to operate only on certain lines. The following example removes the 4th line only. [jerry]$ sed ”4d” books.txt On executing the above code, you get the following result: 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 Additionally, SED also accepts address range using comma(,). We can instruct the SED to remove N1 to N2 lines. For instance, the following example deletes all the lines from 2 through 4. [jerry]$ sed ”2, 4 d” books.txt On executing the above code, you get the following result: 1) A Storm of Swords, George R. R. Martin, 1216 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 SED”s address range is not only limited to numbers. We can also specify patterns as an address. The following example removes all the books of the author Paulo Coelho. [jerry]$ sed ”/Paulo Coelho/d” books.txt On executing the above code, you get the following result: 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones, George R. R. Martin, 864 We can also specify an address range using textual pattern. The following example removes all lines between the patterns Storm and Fellowship. [jerry]$ sed ”/Storm/,/Fellowship/d” books.txt 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 In addition to this, we can also use dollar($), plus(+), and tilde(~) operators with SED. Write Command One of the important operations we perform on any file is backup, i.e., we make another copy of the file. SED provides the write command to store the contents of the pattern buffer in a file. Given below is the syntax of the write command which is similar to the delete command. [address1[,address2]]w file Here, address1 and address2 are the starting and the ending address respectively, which can be either line numbers or pattern strings. Both of these addresses are optional parameters. In the above syntax, w refers to the write command and file is the file name in which you store contents. Be careful with the file parameter. When a file name is provided, the SED creates a file on the fly if it is not present, and overwrites it if it is already present. Let us make an exact copy of the file using SED. Note that there must be exactly one space between w and file. [jerry]$ sed -n ”w books.bak” books.txt We created another file called books.bak. Now verify that both the files have identical content. [jerry]$ diff books.txt books.bak [jerry]$ echo $? On executing the above code, you get the following result: 0 You may assume that the cp command does exactly the same thing. Yes! The cp command does the same thing, but SED is a matured utility. It allows creating a file containing only certain lines from the source file. Let us store only even lines to another file. [jerry]$ sed -n ”2~2 w junk.txt” books.txt [jerry]$ cat junk.txt On executing the above code, you get the following result: 2) The Two Towers, J. R. R. Tolkien, 352 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones, George R. R. Martin, 864 You can also use comma(,), dollar($), and plus(+) operators with the write command. In addition to this, SED also supports pattern matching with the write command. Suppose you want to store all the books of individual authors into a separate file. One boring and lengthy way is do it manually, and the smarter way is to use SED. [jerry]$ sed -n -e ”/Martin/ w Martin.txt” -e ”/Paulo/ w Paulo.txt” -e ”/Tolkien/ w Tolkien.txt” books.txt In the above example, we are matching each line against a pattern and storing the matched line in a particular file. It is very simple. To specify multiple commands, we used -e switch of the SED command. Now let use see what each file contains: [jerry]$ cat Martin.txt On executing the above code, you get the following result: 1) A Storm of Swords, George R. R. Martin, 1216 6) A Game of Thrones, George R. R. Martin, 864 Let us display the file contents. [jerry]$ cat Paulo.txt On executing the above code, you get the following result: 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288 Let us display the file contents. [jerry]$ cat Tolkien.txt On executing the above code, you get the following result: 2) The Two Towers, J. R. R. Tolkien, 352 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 Excellent! We got the expected result. SED is
Sed – Pattern Buffer
Stream Editor – Pattern Buffer ”; Previous Next One of the basic operations we perform on any file is display its contents. For this purpose, we can use the print command which prints the contents of the pattern buffer. So let us learn more about the pattern buffer First create a file containing the line number, the name of the book, its author, and the number of pages. In this tutorial, we will be using this file. You can use any text file according to your convenience. Our text file will look like this: [jerry]$ vi books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho,288 6) A Game of Thrones, George R. R. Martin, 864 Now, let us print the file contents. [jerry]$ sed ”p” books.txt When the above code is executed, it will produce the following result. 1) A Storm of Swords, George R. R. Martin, 1216 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 6) A Game of Thrones, George R. R. Martin, 864 You might wonder why each line is being displayed twice. Let us find out. Do you remember the workflow of SED? By default, SED prints the contents of the pattern buffer. In addition, we have included a print command explicitly in our command section. Hence each line is printed twice. But don”t worry. SED has the -n option to suppress the default printing of the pattern buffer. The following command illustrates that. [jerry]$ sed -n ”p” books.txt When the above code is executed, it will produce the following result. 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 Congratulations! we got the expected result. By default, SED operates on all lines. But we can force SED to operate only on certain lines. For instance, in the example below, SED only operates on the 3rd line. In this example, we have specified an address range before the SED command. [jerry]$ sed -n ”3p” books.txt When the above code is executed, it will produce the following result. 3) The Alchemist, Paulo Coelho, 197 Additionally, we can also instruct SED to print only certain lines. For instance, the following code prints all the lines from 2 to 5. Here we have used the comma(,) operator to specify the address range. [jerry]$ sed -n ”2,5 p” books.txt When the above code is executed, it will produce the following result. 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 There is also a special character Dollar($) which represents the last line of the file. So let us print the last line of the file. [jerry]$ sed -n ”$ p” books.txt When the above code is executed, it will produce the following result. 6) A Game of Thrones, George R. R. Martin, 864 However we can also use Dollar($) character to specify address range. Below example prints through line 3 to last line. [jerry]$ sed -n ”3,$ p” books.txt When the above code is executed, it will produce the following result. 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 We learnt how to specify an address range using the comma(,) operator. SED supports two more operators that can be used to specify address range. First is the plus(+) operator and it can be used with the comma(,) operator. For instance M, +n will print the next n lines starting from line number M. Sounds confusing? Let us check it with a simple example. The following example prints the next 4 lines starting from line number 2. [jerry]$ sed -n ”2,+4 p” books.txt When the above code is executed, it will produce the following result. 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 Optionally, we can also specify address range using the tilde(~) operator. It uses M~n form. It indicates that SED should start at line number M and process every n(th) line. For instance, 50~5 matches line number 50, 55, 60, 65, and so on. Let us print only odd lines from the file. [jerry]$ sed -n ”1~2 p” books.txt When the above code is executed, it will produce the following result. 1) A Storm of Swords, George R. R. Martin, 1216 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288 The following code prints only even lines from the file. [jerry]$ sed -n ”2~2 p” books.txt When the above code is executed, it will produce the following result. 2) The Two Towers, J. R. R. Tolkien, 352 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones, George R. R. Martin, 864 Print Page