Sed – Workflow

Stream Editor – Workflow ”; Previous Next 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 the pattern buffer. Initially both pattern and hold buffers are empty. If no input files are provided, then SED accepts input from the standard input stream (stdin). If address range is not provided by default, then SED operates on each line. Examples Let us create a text file quote.txt to contain a quote of the famous author Paulo Coelho. [jerry]$ vi quote.txt There is only one thing that makes a dream impossible to achieve: the fear of failure. – Paulo Coelho, The Alchemist To understand the workflow of SED, let us display the contents of the file quote.txt using SED. This example simulates the cat command. [jerry]$ sed ”” quote.txt When the above code is executed, it will produce the following result. There is only one thing that makes a dream impossible to achieve: the fear of failure. In the above example, quote.txt is the input file name and before that there is a pair of single quote that implies the SED command. Let us demystify this operation. First SED reads a line from the input file quote.txt and stores it in its pattern buffer. Then it applies SED commands on the pattern buffer. In our case, no SED commands are there, hence no operation is performed on the pattern buffer. Finally it deletes and prints the contents of the pattern buffer on the standard output. Isn”t it simple? In the following example, SED accepts input from the standard input stream. [jerry]$ sed ”” When the above code is executed, it will produce the following result. There is only one thing that makes a dream impossible to achieve: the fear of failure. There is only one thing that makes a dream impossible to achieve: the fear of failure. Here, the first line is entered through keyboard and the second is the output generated by SED. To exit from the SED session, press ctrl-D (^D). Print Page Previous Next Advertisements ”;

Sed – Loops

Stream Editor – Loops ”; Previous Next Like other programming languages, SED too provides a looping and branching facility to control the flow of execution. In this chapter, we are going to explore more about how to use loops and branches in SED. A loop in SED works similar to a goto statement. SED can jump to the line marked by the label and continue executing the remaining commands. In SED, we can define a label as follows: :label :start :end :up In the above example, a name after colon(:) implies the label name. To jump to a specific label, we can use the b command followed by the label name. If the label name is omitted, then the SED jumps to the end of the SED file. Let us write a simple SED script to understand the loops and branches. In our books.txt file, there are several entries of book titles and their authors. The following example combines a book title and its author name in one line separated by a comma. Then it searches for the pattern “Paulo”. If the pattern matches, it prints a hyphen(-) in front of the line, otherwise it jumps to the Print label which prints the line. [jerry]$ sed -n ” h;n;H;x s/n/, / /Paulo/!b Print s/^/- / :Print 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 At first glance, the above script may look cryptic. Let us demystify this. The first two commands are self-explanatory h;n;H;x and s/n/, / combine the book title and its author separated by a comma(,). The third command jumps to the label Print only when the pattern does not match, otherwise substitution is performed by the fourth command. :Print is just a label name and as you already know, p is the print command. To improve readability, each SED command is placed on a separate line. However, one can choose to place all the commands in one line as follows: [jerry]$ sed -n ”h;n;H;x;s/n/, /;/Paulo/!b Print; s/^/- /; :Print;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 Print Page Previous Next Advertisements ”;

Scala Collections – reduce

Scala Collections – Reduce Method ”; Previous Next reduce() method is a member of TraversableOnce trait, it is used to collapse elements of collections. It is similar to fold method but it does not take initial value. Syntax The following is the syntax of reduce method. def reduce[A1 >: A](op: (A1, A1) ? A1): A1 Here, reduce method takes associative binary operator function as a parameter. This method returns the resultant value. Usage Below is an example program of showing how to use fold method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) //apply operation to get sum of all elements of the list val result = list.reduce(_ + _) //print result println(result) } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output 10 Print Page Previous Next Advertisements ”;

Scala Collections – partition

Scala Collections – Partition Method ”; Previous Next partition() method is a member of TraversableLike trait, it is used to run a predicate method on each elements of a collection. It returns two collections, one collection is of elements which satisfiles a given predicate function and another collection is of elements which do not satisfy the given predicate function. Syntax The following is the syntax of map method. def partition(p: (A) ? Boolean): (Repr, Repr) Here, partition method takes a prediate function as a parameter. This method returns the collections. Usage Below is an example program of showing how to use partition method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3, 4, 5, 6, 7) //apply operation to get twice of each element. val (result1, result2) = list.partition(x=>{x % 3 == 0}) //print result println(result1) println(result2) } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output List(3, 6) List(1, 2, 4, 5, 7) Print Page Previous Next Advertisements ”;

Scala Collections – scan

Scala Collections – Scan Method ”; Previous Next scan() method is a member of TraversableLike trait, it is similar to fold method but is used to apply a operation on each elements of collection and return a collection. Syntax The following is the syntax of fold method. def scan[B >: A, That](z: B)(op: (B, B) ? B)(implicit cbf: CanBuildFrom[Repr, B, That]): That Here, scan method takes associative binary operator function as a parameter. This method returns the updated collection as result. It considers first input as initial value and second input as a function. Usage Below is an example program of showing how to use scan method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) //apply operation to create a running total of all elements of the list val list1 = list.scan(0)(_ + _) //print list println(list1) } } Here we”ve passed 0 as initial value to scan function and then all values are added. Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output List(0, 1, 3, 6, 10) Print Page Previous Next Advertisements ”;

Scala Collections – zip

Scala Collections – Zip Method ”; Previous Next zip() method is a member of IterableLike trait, it is used to merge a collection to current collection and result is a collection of pair of tuple elements from both collections. Syntax The following is the syntax of zip method. def zip[B](that: GenIterable[B]): Iterable[(A, B)] Here, zip method takes a collection as parameter. This method returns the updated collection of pair as result. Usage Below is an example program of showing how to use zip method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) val list1 = List(“A”, “B”, “C”, “D”) //apply operation to create a zip of list val list2 = list zip list1 //print list println(list2) } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output List((1,A), (2,B), (3,C), (4,D)) Print Page Previous Next Advertisements ”;

Scala Collections – HashSet

Scala Collections – HashSet ”; Previous Next Scala Set is a collection of pairwise different elements of the same type. In other words, a Set is a collection that contains no duplicate elements. HashSet implements immutable sets and uses hash table. Elements insertion order is not preserved. Declaring HashSet Variables The following is the syntax for declaring an HashSet variable. Syntax var z : HashSet[String] = HashSet(“Zara”,”Nuha”,”Ayan”) Here, z is declared as an hash-set of Strings which has three members. Values can be added by using commands like the following − Command var myList1: HashSet[String] = myList + “Naira”; Processing HashSet Below is an example program of showing how to create, initialize and process HashSet − Example import scala.collection.immutable.HashSet object Demo { def main(args: Array[String]) = { var mySet: HashSet[String] = HashSet(“Zara”,”Nuha”,”Ayan”); // Add an element var mySet1: HashSet[String] = mySet + “Naira”; // Remove an element var mySet2: HashSet[String] = mySet – “Nuha”; // Create empty set var mySet3: HashSet[String] = HashSet.empty[String]; println(mySet); println(mySet1); println(mySet2); println(mySet3); } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output HashSet(Zara, Nuha, Ayan) HashSet(Zara, Nuha, Ayan, Naira) HashSet(Zara, Ayan) HashSet() Print Page Previous Next Advertisements ”;

Rust – Discussion

Discuss Rust ”; Previous Next Rust is a modern systems programming language developed by the Mozilla Corporation. It is intended to be a language for highly concurrent and highly secure systems. It compiles to native code; hence, it is blazingly fast like C and C++. This tutorial adopts a simple and practical approach to describe the concepts of Rust programming. Print Page Previous Next Advertisements ”;

Scala Collections – Stack

Scala Collections – Stack ”; Previous Next Stack is Last In First Out, LIFO data structure and allows to insert and retrieve element at top, in LIFO manner. Declaring Stack Variables The following is the syntax for declaring an Stack variable. Syntax val stack = Stack(1, 2, 3, 4, 5) Here, stack is declared as an Stack of numbers. Value can be added at top by using commands like the following − Command stack.push(6) Value can be retrived from top by using commands like the following − Command stack.top Value can be removed from top by using commands like the following − Command stack.pop Processing Stack Below is an example program of showing how to create, initialize and process Stack − Example import scala.collection.mutable.Stack object Demo { def main(args: Array[String]) = { var stack: Stack[Int] = Stack(); // Add elements stack.push(1); stack.push(2); // Print element at top println(“Top Element: ” + stack.top) // Print element println(“Removed Element: ” + stack.pop()) // Print element println(“Top Element: ” + stack.top) } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output Top Element: 2 Removed Element: 2 Top Element: 1 Print Page Previous Next Advertisements ”;

Scala Collections – foldLeft

Scala Collections – FoldLeft Method ”; Previous Next foldLeft() method is a member of TraversableOnce trait, it is used to collapse elements of collections. It navigates elements from Left to Right order. It is primarily used in recursive functions and prevents stack overflow exceptions. Syntax The following is the syntax of fold method. def foldLeft[B](z: B)(op: (B, A) ? B): B Here, foldLeft method takes associative binary operator function as a parameter. This method returns the result as value. Usage Below is an example program of showing how to use foldLeft method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) //apply operation to get sum of all elements of the list val result = list.foldLeft(0)(_ + _) //print result println(result) } } Here we”ve passed 0 as initial value to fold function and then all values are added. Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output 10 Print Page Previous Next Advertisements ”;