Saturday, June 15, 2013

Useful Options of sed command

          How to use sed, a special editor for modifying files automatically. If you want to write a program to make changes in a file, sed is the tool to use. Sed is the ultimate stream editor. If that sounds strange, picture a stream flowing through a pipe. sed is a marvelous utility.

The essential command: s for substitution:
            Sed has several commands, but most people only use substitute: s. The substitute command changes all occurrences of the regular expression into a new value. A simple example is changing "day" in the "old" file to "night" in the "new" file.
This will output "night".
sed 's/day/night/' File_1 >File_2
I must emphasize that the sed editor changes exactly what you tell it to. So if you executed
echo Sunday | sed 's/day/rise/'
This would output the word "Sunrise" because sed found the string "day" in the input.
Another important concept is that sed is line oriented. Suppose you have the input file:
one two three, one two three
four three two one
one hundred
and you used the command
sed 's/one/ONE/' <file
The output would be :
ONE two three, one two three
four three two ONE
ONE hundred
Note :You can also change delimiter "/"  to ":" like sed 's:one:ONE:' 
s/pattern/replacement/flags
Substitutes the replacement string for the first occurrence of the pattern parameter in the pattern space. Any character that is displayed after the s
subcommand can substitute for the / (slash) separator except for the space or new-line character.
 See the "Pattern Matching" section of the ed command.
 The value of the flags variable must be zero or more of:
g :Substitutes all non-overlapping instances of the pattern parameter rather than just the first one.
n :Substitutes for the n-th occurrence only of the pattern parameter.
p :Writes the pattern space to standard output if a replacement was made.
w WFile :Writes the pattern space to the WFile variable if a replacement was made. Appends the pattern space to the WFile variable. If the WFile variable was not already created by a previous write by this sed script, the sed command creates it.
Flags:
       -e Script :Uses the Script variable as the editing script. If you are using just one -e flag and no -f flag, the -e flag can be omitted.
       -f  ScriptFile :Uses the ScriptFile variable as the source of the edit script. The ScriptFile variable is a prepared set of editing commands applied to the File
            parameter.
       -n :Suppresses all information normally written to standard output.
       -u :Displays the output in an un buffered mode. When this flag is set, the sed command displays the output instantaneously instead of buffering the output. The default is buffered mode.  

Note: You can specify multiple -e and -f flags. All sub commands are added to the script in the order specified, regardless of their origin.



0 comments:

 

Datastage Doctrina Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger

Receive all updates, tips and tricks via Facebook. Just Click the Like Button Below