Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

unix cmd doubt

+2
Mahee
anand
6 posters

Go down

unix cmd doubt Empty unix cmd doubt

Post by anand Wed Mar 03, 2010 4:46 pm

wat does this cmd do..
cat /etc/shells | sed 's/bash/BASH/g'
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

unix cmd doubt Empty reply

Post by Mahee Wed Mar 03, 2010 6:11 pm

It means that
display the content of etc/shells in which replace all 'bash' with 'BASH'
Mahee
Mahee

Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by anand Wed Mar 03, 2010 6:24 pm

k.. fine... but wats meanin of s and g in line ==> 's/bash/BASH/g'
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Christopher Wed Mar 03, 2010 6:32 pm

i guess s stands for substitute and g stands for all instance in a line. But im not sure.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Maithreyi Wed Mar 03, 2010 6:36 pm

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

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Mahee Wed Mar 03, 2010 6:38 pm

yes christopher is correct..
's' stands for substitute and 'g' stands for global [all matching occurrences ]
Mahee
Mahee

Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by anand Wed Mar 03, 2010 6:46 pm

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 ?
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Mahee Wed Mar 03, 2010 6:54 pm

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


Last edited by Mahee on Wed Mar 03, 2010 6:57 pm; edited 2 times in total
Mahee
Mahee

Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by urvershi Wed Mar 03, 2010 6:56 pm

'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

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Christopher Wed Mar 03, 2010 6:58 pm

y stands for transform. but i cant find the diff between s and y.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Mahee Wed Mar 03, 2010 7:06 pm

in 'y each character is replaced
and in 's the entire string is replaced
Mahee
Mahee

Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Christopher Wed Mar 03, 2010 7:08 pm

yup Smile
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by ARVINTH B.J Thu Mar 04, 2010 2:18 pm

$ 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

ARVINTH B.J

Posts : 20
Points : 31
Join date : 2010-02-26

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by anand Thu Mar 04, 2010 3:32 pm

cat /etc/shells |sed 's/bin/BIN/cg' ===> s not workin ...

how to replace globally with confirmation
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by urvershi Thu Mar 04, 2010 3:52 pm

'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

urvershi

Posts : 21
Points : 70
Join date : 2010-02-26

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by anand Thu Mar 04, 2010 3:58 pm

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?
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Mahee Thu Mar 04, 2010 4:14 pm

@ 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
Mahee
Mahee

Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Mahee Thu Mar 04, 2010 4:29 pm

@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......
Mahee
Mahee

Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia

Back to top Go down

unix cmd doubt Empty Re: unix cmd doubt

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum