Total Pageviews

Thursday, March 15, 2012

sed KB

http://www.thegeekstuff.com/2009/10/unix-sed-tutorial-advanced-sed-substitution-examples/
http://www.grymoire.com/Unix/Sed.html
http://ss64.com/nt/syntax-esc.html


# substitute (find and replace) "foo" with "bar" on each line *******

sed 's/foo/bar/' # replaces only 1st instance in a line
sed 's/foo/bar/4' # replaces only 4th instance in a line
sed 's/foo/bar/g' # replaces ALL instances in a line
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
sed 's/\(.*\)foo/\1bar/' # replace only the last case

# substitute "foo" with "bar" ONLY for lines which contain "baz"

sed '/baz/s/foo/bar/g'

# substitute "foo" with "bar" EXCEPT for lines which contain "baz"

sed '/baz/!s/foo/bar/g'

# change "scarlet" or "ruby" or "puce" to "red"

sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' # most seds


Remove M Characters (^M) with Unix commands


Using dos2unix command

dos2unix ORIG_FILENAME TEMP_FILENAME

mv TEMP_FILENAME ORIG_FILENAME

IMP NOTE: use dos2ux command if you are using HPUX

Using sed command

sed ‘s/^M//g’ ORIG_FILENAME > TEMP_FILENAME

mv TEMP_FILENAME ORIG_FILENAME

IMP NOTE: To get ^M in UNIX (Hold control key and then press v and m character).

Using vi Editor

ESCAPE :%s/^M//g ENTER

IMP NOTE: To get ^M in UNIX (Hold control key and then press v and m character).

No comments:

Post a Comment