Home » 2014 » April » 10 » SED command for filtering and transforming texts of a file using regular expression

8:47 PM
SED command for filtering and transforming texts of a file using regular expression

 

       sed - stream editor for filtering and transforming text

FORMAT
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...

DESCRIPTION
       Sed  is  a stream editor.  A stream editor is used 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.

Some useful parameters:

 -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if suffix supplied). So if the input file is abc.txt, a backup file will be created as abc.txtSUFFIX 
 -f script-file, --file=script-file

              add the contents of script-file to the commands to be executed. This file contains the regular exp and sunstitution word. used for SED
 s/regexp/replacement/
              Attempt  to  match  regexp  against the pattern space.  If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched,  and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.

Eg. Suppose we have file abc.txt like this:

/home/user1:> cat abc.txt
old new is gold
gold is old
new is old
gold is not old

 

I want to find and replace "old" by "new". So I will use below command.
  sed -f  <script-file> -i[SUFFIX] <inputfile>
  sed -f  somesed.sed
i.201404100831 abc.txt

somesed.sed is containing:

:/home/user1:> cat somesed.sed
s/old/new/

 

So above command will use the somesed.sed file as script file and process the input file abc.txt and later will create a backup file as abc.txt.201404100831 . See the result:

/home/user1:> sed -f somesed.sed -i.201404100831 abc.txt
home/user1:> cat abc.txt
new new is gold
gnew is old
new is new
gnew is not old

The backup file:
home/user1:> cat abc.txt.201404100831
old new is gold
gold is old
new is old
gold is not old

 

Go to Next session..

 
 

Category: Open System-Linux | Views: 1563 | Added by: shanky | Tags: sed command in linux, sed in linux. sed in unix, how to find and replace a word by a, replace a word by another using SED | Rating: 0.0/0

Related blogs


You may also like to see:


[2014-05-22][Open System-Linux]
Archiving and compression of files in Linux
[2014-04-22][Open System-Linux]
xmllint : a command line xml tool to view and find errors in XML file
[2015-01-12][Open System-Linux]
W3M : A text based web browser and pager from Linux terminal
[2014-03-12][Open System-Linux]
uptime command in Linux
[2014-10-17][Open System-Linux]
SETFACL command in Linux to set file access control list

Total comments: 0
ComForm">
avatar