unix cmd doubt
+2
Mahee
anand
6 posters
unix cmd doubt
wat does this cmd do..
cat /etc/shells | sed 's/bash/BASH/g'
cat /etc/shells | sed 's/bash/BASH/g'
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
reply
It means that
display the content of etc/shells in which replace all 'bash' with 'BASH'
display the content of etc/shells in which replace all 'bash' with 'BASH'
Mahee- Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia
Re: unix cmd doubt
k.. fine... but wats meanin of s and g in line ==> 's/bash/BASH/g'
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: unix cmd doubt
i guess s stands for substitute and g stands for all instance in a line. But im not sure.
Re: unix cmd doubt
s stands for substitute.It replaces the first occurence alone.g stands for global replace.It replaces all occurences!
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: unix cmd doubt
yes christopher is correct..
's' stands for substitute and 'g' stands for global [all matching occurrences ]
's' stands for substitute and 'g' stands for global [all matching occurrences ]
Mahee- Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia
Re: unix cmd doubt
cat /etc/shells |sed 'y/ab/AB/'
output:
/Bin/sh
/Bin/BAsh
/sBin/nologin
/Bin/tcsh
/Bin/csh
/Bin/ksh
it replaces all a n b wit A n B......
but wat does "y " in command mean ?
output:
/Bin/sh
/Bin/BAsh
/sBin/nologin
/Bin/tcsh
/Bin/csh
/Bin/ksh
it replaces all a n b wit A n B......
but wat does "y " in command mean ?
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: unix cmd doubt
I Think the below content with help you with sed command
UNIX --> SED
-------------+----------------------------------------------------------------
cat --> sed ':'
cat -s --> sed '1s/^$//p;/./,/^$/!d'
tac --> sed '1!G;h;$!d'
grep --> sed '/patt/!d'
grep -v --> sed '/patt/d'
head --> sed '10q'
head -1 --> sed 'q'
tail --> sed -e ':a' -e '$q;N;11,$D;ba'
tail -1 --> sed '$!d'
tail -f --> sed -u '/./!d'
cut -c 10 --> sed 's/\(.\)\{10\}.*/\1/'
cut -d: -f4 --> sed 's/\(\([^:]*\):\)\{4\}.*/\2/'
tr A-Z a-z --> sed 'y/ABC/abc'
tr a-z A-Z --> sed 'y/abc/ABC/'
tr -s ' ' --> sed 's/ \+/ /g'
tr -d '\012'--> sed 'H;$!d;g;s/\n//g'
wc -l --> sed -n '$='
uniq --> sed 'N;/^\(.*\)\n\1$/!P;D'
rev --> sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
basename --> sed 's,.*/,,'
dirname --> sed 's,[^/]*$,,'
xargs --> sed -e ':a' -e '$!N;s/\n/ /;ta'
paste -sd: --> sed -e ':a' -e '$!N;s/\n/:/;ta'
cat -n --> sed '=' | sed '$!N;s/\n/ /'
grep -n --> sed -n '/patt/{=;p;}' | sed '$!N;s/\n/:/'
cp orig new--> sed 'w new' orig
UNIX --> SED
-------------+----------------------------------------------------------------
cat --> sed ':'
cat -s --> sed '1s/^$//p;/./,/^$/!d'
tac --> sed '1!G;h;$!d'
grep --> sed '/patt/!d'
grep -v --> sed '/patt/d'
head --> sed '10q'
head -1 --> sed 'q'
tail --> sed -e ':a' -e '$q;N;11,$D;ba'
tail -1 --> sed '$!d'
tail -f --> sed -u '/./!d'
cut -c 10 --> sed 's/\(.\)\{10\}.*/\1/'
cut -d: -f4 --> sed 's/\(\([^:]*\):\)\{4\}.*/\2/'
tr A-Z a-z --> sed 'y/ABC/abc'
tr a-z A-Z --> sed 'y/abc/ABC/'
tr -s ' ' --> sed 's/ \+/ /g'
tr -d '\012'--> sed 'H;$!d;g;s/\n//g'
wc -l --> sed -n '$='
uniq --> sed 'N;/^\(.*\)\n\1$/!P;D'
rev --> sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
basename --> sed 's,.*/,,'
dirname --> sed 's,[^/]*$,,'
xargs --> sed -e ':a' -e '$!N;s/\n/ /;ta'
paste -sd: --> sed -e ':a' -e '$!N;s/\n/:/;ta'
cat -n --> sed '=' | sed '$!N;s/\n/ /'
grep -n --> sed -n '/patt/{=;p;}' | sed '$!N;s/\n/:/'
cp orig new--> sed 'w new' orig
Last edited by Mahee on Wed Mar 03, 2010 6:57 pm; edited 2 times in total
Mahee- Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia
Re: unix cmd doubt
's' stands for substitute and 'g' stands for global [all matching occurrences ] and replace them globally without confirmation and 'cg' replace globally with confirmation.
urvershi- Posts : 21
Points : 70
Join date : 2010-02-26
Re: unix cmd doubt
in 'y each character is replaced
and in 's the entire string is replaced
and in 's the entire string is replaced
Mahee- Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia
Re: unix cmd doubt
$ cat /etc/shells |sed 's/bin/BIN/g'.............................
/BIN/sh
/BIN/bash
/sBIN/nologin
/BIN/tcsh
/BIN/csh
/BIN/ksh
$ cat /etc/shells |sed 's/bin/BIN/' .................
/BIN/sh
/BIN/bash
/sBIN/nologin
/BIN/tcsh
/BIN/csh
/BIN/ksh
whats the diff bet these two
/BIN/sh
/BIN/bash
/sBIN/nologin
/BIN/tcsh
/BIN/csh
/BIN/ksh
$ cat /etc/shells |sed 's/bin/BIN/' .................
/BIN/sh
/BIN/bash
/sBIN/nologin
/BIN/tcsh
/BIN/csh
/BIN/ksh
whats the diff bet these two
ARVINTH B.J- Posts : 20
Points : 31
Join date : 2010-02-26
Re: unix cmd doubt
cat /etc/shells |sed 's/bin/BIN/cg' ===> s not workin ...
how to replace globally with confirmation
how to replace globally with confirmation
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: unix cmd doubt
'cg' option is not working with sed command on command prompt but it is working in vi editor
syntax:
:$s/word-to-find/word-to-replace/cg
syntax:
:$s/word-to-find/word-to-replace/cg
urvershi- Posts : 21
Points : 70
Join date : 2010-02-26
Re: unix cmd doubt
k..fine .. i tried like this ...
cat /etc/shells | sed 'c/bin/BIN/g' =====>
ouput:
/bin/BIN/g
/bin/BIN/g
/bin/BIN/g
/bin/BIN/g
/bin/BIN/g
/bin/BIN/g
how does this work.. any idea?
cat /etc/shells | sed 'c/bin/BIN/g' =====>
ouput:
/bin/BIN/g
/bin/BIN/g
/bin/BIN/g
/bin/BIN/g
/bin/BIN/g
/bin/BIN/g
how does this work.. any idea?
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: unix cmd doubt
@ aravinth
The difference between with 'g' and without 'g' is that
without g => it will find only the first occurance of the string in each line.
with g => it will find all the occurances of the string in each line
the example you tried contains only one occurance of the string 'bin' in each line. so both the commands work in the same way.
Try both the commands to the lines having more than one occurances of the string so that you can know the difference
The difference between with 'g' and without 'g' is that
without g => it will find only the first occurance of the string in each line.
with g => it will find all the occurances of the string in each line
the example you tried contains only one occurance of the string 'bin' in each line. so both the commands work in the same way.
Try both the commands to the lines having more than one occurances of the string so that you can know the difference
Mahee- Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia
Re: unix cmd doubt
@anand
'sed c' is the replacement tool
syntax is sed '<<line no>>c <<new string>>'
for eg
try
cat /etc/shells | sed 'c My Name is Anand'
and
cat /etc/shells | sed '3c My Name is Anand'
then U will find the answer......
'sed c' is the replacement tool
syntax is sed '<<line no>>c <<new string>>'
for eg
try
cat /etc/shells | sed 'c My Name is Anand'
and
cat /etc/shells | sed '3c My Name is Anand'
then U will find the answer......
Mahee- Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia
Permissions in this forum:
You cannot reply to topics in this forum
|
|