2020年8月18日星期二

Shell Command: sed.For replace string.

sed : for replace string.

Example1.
 From xml find 
between <xml:user-modules> and </xml:user-modules> 
 delete this xml snippet
sed -e '/\<xml:user-modules>/,/\/xml:user-modules\>/ d' test.xml >result.xml

Example2.
 From xml find 
between <xml:user-modules> and </xml:user-modules> 
 replace user: to customer
sed -e '/\<xml:user-modules>/,/\/xml:user-modules\>/ s/user/customer/g' test.xml >result.xml

$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
========================================
Examples Of Using:
$ cat data1
0000
0001
0002
0003
0004

$ sed -e /0$/i\#Comment data1  #-e script:start from line0 to $end, -i:before 
match line
#Comment
0000
0001
0002
0003
0004

$ sed -e /3$/a\xyz data1  #-e script:start from line3 to $end, -a:after 
match line
0000
0001
0002
0003
xyz
0004

$ sed -e /[1-3]/axyz data1  #-e script:start from line1 to 3, -a:after 
match line
0000
0001
xyz
0002
xyz
0003
xyz
0004

$cat text.txt
DAT00000000

DAT00000001
$ sed -ie '/^$/d'  text.txt  #-e:script, -d:delete, /^$/empty line. -i:before match line
DAT00000000
DAT00000001
$ cat text.txt | sed -e '/DAT//g'  #-e:script, -g:global, /pattern/replace/
00000000
00000001
$ cat text.txt | sed -e 's/DAT/DT/g'  #-e:script, -s:single, -g:global, /pattern/replace/
DT00000000
DT00000001



没有评论: