পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Monday, April 3, 2017

Stream Editor - Basic Syntax

This chapter introduces the basic commands that SED supports and their command-line syntax. SED can be invoked in the following two forms:
sed [-n] [-e] 'command(s)' files 
sed [-n] -f scriptfile files
The first form allows to specify the commands in-line and they are enclosed within single quotes. The later allows to specify a script file that contains SED commands. However, we can use both forms together multiple times. SED provides various command-line options to control its behavior.
Let us see how we can specify multiple SED commands. SED provides the delete command to delete certain lines. Let us delete the 1st, 2nd, and 5th lines. For the time being, ignore all the details of the delete command. We will discuss more about the delete command later.
First, display the file contents using the cat command.
[jerry]$ cat 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 instruct SED to remove only certain lines. Here, to delete three lines, we have specified three separate commands with -e option.
[jerry]$ sed -e '1d' -e '2d' -e '5d' 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 
6) A Game of Thrones, George R. R. Martin, 864 
Additionally, we can write multiple SED commands in a text file and provide the text file as an argument to SED. SED can apply each command on the pattern buffer. The following example illustrates the second form of SED.
First, create a text file containing SED commands. For easy understanding, let us use the same SED commands.
[jerry]$ echo -e "1d\n2d\n5d" > commands.txt 
[jerry]$ cat commands.txt
On executing the above code, you get the following result:
1d 
2d 
5d 
Now instruct the SED to read commands from the text file. Here, we achieve the same result as shown in the above example.
[jerry]$ sed -f commands.txt 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 
6) A Game of Thrones,George R. R. Martin, 864 

Standard Options

SED supports the following standard options:
  • -n: Default printing of pattern buffer. For example, the following SED command does not show any output:
  • [jerry]$ sed -n '' quote.txt 
    
  • -e : Next argument is an editing command. Here, angular brackets imply mandatory parameter. By using this option, we can specify multiple commands. Let us print each line twice:
  • [jerry]$ sed -e '' -e 'p' quote.txt
    
On executing the above code, you get 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. 
 - Paulo Coelho, The Alchemist 
 - Paulo Coelho, The Alchemist
  • -f : Next argument is a file containing editing commands. The angular brackets imply mandatory parameter. In the following example, we specify print command through file:
[jerry]$ echo "p" > commands 
[jerry]$ sed -n -f commands quote.txt
On executing the above code, you get the following result:
There is only one thing that makes a dream impossible to achieve: the fear of failure. 
 - Paulo Coelho, The Alchemist

GNU Specific Options

Let us quickly go through the GNU specific SED options. Note that these options are GNU specific; and may not be supported by other variants of the SED. In later sections, we will discuss these options in more details.
  • -n, --quiet, --silent: Same as standard -n option.
  • -e script, --expression=script: Same as standard -e option.
  • -f script-file, --file=script-file: Same as standard -f option.
  • --follow-symlinks: If this option is provided, the SED follows symbolic links while editing files in place.
  • -i[SUFFIX], --in-place[=SUFFIX]: This option is used to edit file in place. If suffix is provided, it takes a backup of the original file, otherwise it overwrites the original file.
  • -l N, --line-lenght=N: This option sets the line length for l command to N characters.
  • --posix: This option disables all GNU extensions.
  • -r, --regexp-extended: This option allows to use extended regular expressions rather than basic regular expressions.
  • -u, --unbuffered: When this option is provided, the SED loads minimal amount of data from the input files and flushes the output buffers more often. It is useful for editing the output of "tail -f" when you do not want to wait for the output.
  • -z, --null-data: By default, the SED separates each line by a new-line character. If NULL-data option is provided, it separates the lines by NULL characters.

No comments:

Post a Comment