SED eng

From ICO wiki
Revision as of 20:10, 8 May 2017 by Gadoyi (talk | contribs)
Jump to navigationJump to search

SED - sed is a stream editor. The major use of stream editor is to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors[1]. it basically means that all the editing are made by calling the command and sed will execute the directions automatically. sed is a very powerful and fast way to transform text.

Introduction

sed stream editor is a text editor that performs editing operations on information coming from standard input or a file. Sed reads and edits text line-by-line and in a non-interactive way.[2]

Main Operation and usage

sed is a line-oriented text processing utility: it reads text, line by line, from an input stream or file, into an internal buffer called the pattern space. Each line read starts a cycle. To the pattern space, sed applies one or more operations which have been specified via a sed script. sed implements a programming language with about 25 commands that specify the operations on the text. For each input line, after running the script sed ordinarily outputs the pattern space (the line as modified by the script) and begins the cycle again with the next line. Other end-of-script behaviors are available through sed options and script commands, e.g. d to delete the pattern space, q to quit, N to add the next line to the pattern space immediately, and so on. Thus a sed script corresponds to the body of a loop that iterates through the lines of a stream, where the loop itself and the loop variable (the current line number) are implicit and maintained by sed.

The sed script can either be specified on the command line (-e option) or read from a separate file (-f option). Commands in the sed script may take an optional address, in terms of line numbers or regular expressions. The address determines when the command is run. For example, 2d would only run the d (delete) command on the second input line (printing all lines but the second), while /^ /d would delete all lines beginning with a space.[3]

In general, sed operates on a stream of text that it reads from either standard input or from a file.which means that all the editing are made by calling the command and sed will execute the directions automatically. Have it in mind that sed outputs everything to standard out by default. which means that, unless redirected, sed print its output to the screen instead of saving it in a file.

RUNNING SED

Sed can be run and invoked as follow

   sed SCRIPT INPUTFILE.

In order to replace all occurrences of ‘hello’ to ‘world’ in the file input.txt:

   sed 's/hello/world/' input.txt > output.txt.

If an INPUTFILE is not specified, or if INPUTFILE is -, sed filters the contents of the standard input. The following commands are thesame:

   sed 's/hello/world/' input.txt > output.txt
   sed 's/hello/world/' < input.txt > output.txt
   cat input.txt | sed 's/hello/world/' - > output.txt.

sed writes output to standard output. Use -i to edit files in-place instead of printing to standard output. See also the W and s///w commands for writing output to other files. The following command modifies file.txt and does not produce any output:

   sed -i 's/hello/world' file.txt

By default sed prints all processed input (except input that has been modified/deleted by commands such as d). Use -n to suppress output, and the p command to print specific lines. The following command prints only line 50 of the input file

   sed -n '50p' file.txt

sed treats multiple input files as one long stream. The following example prints the first line of the first file (one.txt) and the last line of the last file (three.txt). Use -s to reverse this behavior.

   sed -n  '1p ; $p' one.txt two.txt three.txt

Without -e or -f options, sed uses the first non-option parameter as the script, and the following non-option parameters as input files. If -e or -f options are used to specify a script, all non-option parameters are taken as input files. Options -e and -f can be combined, and can appear multiple times (in which case the final effective script will be concatenation of all the individual scripts).

The following examples are equivalent:

   sed 's/hello/world/' input.txt > output.txt
  
   sed -e 's/hello/world/' input.txt > output.txt
   sed --expression='s/hello/world/' input.txt > output.txt
  
   echo 's/hello/world/' > myscript.sed
   sed -f myscript.sed input.txt > output.txt
   sed --file=myscript.sed input.txt > output.txt


USE AND EDITTING OF FILES

sed enable you to work on a file that you've already created and also sed print its output to the screen instead of saving it in a file. it give you a flexible way to edit a file to the standard you want it to be.

Taking an instance, let's copy some files into our home directory to practice some editing.

   cd
   cp /usr/share/common-licenses/BSD .
   cp /usr/share/common-licenses/GPL-3 .

Now, Let's use sed to view the contents of the BSD license file we copied.

   sed  BSD
   

The output of the file is:

   Copyright (c) The Regents of the University of California.
   All rights reserved.
   
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

Demonstrate how sed can use standard input by piping the output of the "cat" command into sed to produce the same result.

   cat BSD | sed 


   Copyright (c) The Regents of the University of California.
   All rights reserved.
   
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
   . . .
   . . .

As shown below, files can be worked on or streams of text (as is produced when piping output with the "|" character)

Printing Lines

from the previous example, input passed into sed without any operations and the results was prited directly to standard output. when using sed to print, the "print" command, is specified by using the "p" character within single quotes.

   sed 'p' filename

With this print command, sed will print each line twice because it automatically prints each line, note that we've told it to print explicitly with the "p" command. sed operates line by line. It accepts a line, operates on it, and outputs the resulting text before repeating the process on the next line.

The printing results can be clean by passing the "-n" option to sed, which suppresses the automatic printing and output the actual command that its given to print:

   sed -n 'p' filename

This will print out all the lines in the file as it was without duplicating or printing out a specific line.