...making Linux just a little more fun!
Amit Kumar Saha [amitsaha.in at gmail.com]
Hello all,
I have got a shell variable (passed as an argument) which stores some value such as:
$4 = 'abc,def'
Now, I want to replace all the ',' by a ' ' such that the resulting value is 'abc def'.
How do I do it?
This seems to be very basic, so PLEASE do not flame me
I tried doing this:
echo $4 > devlist.tmp #awk script to extract the fields (invididual devices) in the list awk 'BEGIN { FS = "," } ; {print $1, $2 }' devlist.tmp > awk_tmp.tmp devs='cat awk_tmp.tmp'; echo $devs
Seems like I am going now-where.
do suggest a solution!
regards, Amit
-- Amit Kumar Saha *NetBeans Community Docs Coordinator* http://amitsaha.in.googlepages.com http://amitksaha.blogspot.com
Neil Youngman [ny at youngman.org.uk]
On Wednesday 16 April 2008 12:03, Amit Kumar Saha wrote:
> Hello all, > > I have got a shell variable (passed as an argument) which stores some > value such as: > > $4 = 'abc,def' > > Now, I want to replace all the ',' by a ' ' such that the resulting > value is 'abc def'. > > How do I do it?
I would do
var=$(echo $4 | sed -e's/,/ /')
HTH
Neil
Francis Daly [francis at daoine.org]
On Wed, Apr 16, 2008 at 04:33:49PM +0530, Amit Kumar Saha wrote:
Hi there,
> I have got a shell variable (passed as an argument) which stores some > value such as: > > $4 = 'abc,def' > > Now, I want to replace all the ',' by a ' ' such that the resulting > value is 'abc def'. > > How do I do it?
> echo $4 > devlist.tmp > awk 'BEGIN { FS = "," } ; {print $1, $2 }' devlist.tmp > awk_tmp.tmp > devs='cat awk_tmp.tmp'; > echo $devs
You're nearly there.
When you set devs=, you want it to be the output the command, so you must use ` and not ' to surround the command. Or wrap it in $().
Note that your current attempt will only print the first two comma-separated fields -- if your input had been "a,b,c" then your output would have been "a b".
And the initial "echo" may have corrupted any whitespace characters in your input.
> do suggest a solution!
If you want to replace every instance of a single character with another single character, "tr" is the tool to use.
: set the variable var="abc,def',next more" : save the change new=$(echo "$var" | tr , ' ') : show the differences echo :"$var": echo :"$new":
Good luck,
f
-- Francis Daly francis@daoine.org
Kapil Hari Paranjape [kapil at imsc.res.in]
Hello,
On Wed, 16 Apr 2008, Amit Kumar Saha wrote:
> I have got a shell variable (passed as an argument) which stores some > value such as: > > $4 = 'abc,def' > > Now, I want to replace all the ',' by a ' ' such that the resulting > value is 'abc def'. > > How do I do it?
DATA=$4 OLD_IFS="$IFS" { IFS=',' set -- $DATA echo "$1 $2" } IFS="$OLD_IFS"
Regards,
Kapil. --
Thomas Adam [thomas.adam22 at gmail.com]
On 16/04/2008, Francis Daly <francis@daoine.org> wrote:
> If you want to replace every instance of a single character with another > single character, "tr" is the tool to use.
Not really. As a bashism:
[thomas@ubuntu ~]$ A="abc,def,qqq,d" [thomas@ubuntu ~]$ echo ${A//,/ } abc def qqq d
-- Thomas Adam
Ben Okopnik [ben at linuxgazette.net]
On Wed, Apr 16, 2008 at 02:07:30PM +0100, Thomas Adam wrote:
> On 16/04/2008, Francis Daly <francis@daoine.org> wrote: > > If you want to replace every instance of a single character with another > > single character, "tr" is the tool to use. > > Not really. As a bashism: > > `` > [thomas@ubuntu ~]$ A="abc,def,qqq,d" > [thomas@ubuntu ~]$ echo ${A//,/ } > abc def qqq d > ''
That's the one that came to my mind, right away - but when it comes to portability, Francis' method is better. The Bourne shell, or any Bourne derivative will handle 'a=`echo $a|tr , ' '` correctly; even CSH variants will, once you prefix it with 'set'. But it takes a "modern" shell (Bash, KSH, or ZSH) to use the syntax that you suggest.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Thomas Adam [thomas.adam22 at gmail.com]
On 16/04/2008, Ben Okopnik <ben@linuxgazette.net> wrote:
> On Wed, Apr 16, 2008 at 02:07:30PM +0100, Thomas Adam wrote: > > On 16/04/2008, Francis Daly <francis@daoine.org> wrote: > > > If you want to replace every instance of a single character with another > > > single character, "tr" is the tool to use. > > > > Not really. As a bashism: > > > > `` > > [thomas@ubuntu ~]$ A="abc,def,qqq,d" > > [thomas@ubuntu ~]$ echo ${A//,/ } > > abc def qqq d > > '' > > > That's the one that came to my mind, right away - but when it comes to > portability, Francis' method is better. The Bourne shell, or any Bourne > derivative will handle 'a=`echo $a|tr , ' '` correctly; even CSH > variants will, once you prefix it with 'set'. But it takes a "modern" > shell (Bash, KSH, or ZSH) to use the syntax that you suggest.
Yup. That's why I said "bashism". ;)
-- Thomas Adam
Kapil Hari Paranjape [kapil at imsc.res.in]
On Wed, 16 Apr 2008, Ben Okopnik wrote:
> On Wed, Apr 16, 2008 at 02:07:30PM +0100, Thomas Adam wrote: > > Not really. As a bashism: > > > > `` > > [thomas@ubuntu ~]$ A="abc,def,qqq,d" > > [thomas@ubuntu ~]$ echo ${A//,/ } > > abc def qqq d > > '' > > That's the one that came to my mind, right away - but when it comes to > portability, Francis' method is better. The Bourne shell, or any Bourne > derivative will handle 'a=`echo $a|tr , ' '` correctly; even CSH > variants will, once you prefix it with 'set'. But it takes a "modern" > shell (Bash, KSH, or ZSH) to use the syntax that you suggest.
The solution using "set --" and IFS works in any posix shell and does not use any external utilities.
Kapil.
Ben Okopnik [ben at linuxgazette.net]
On Wed, Apr 16, 2008 at 05:31:34PM +0100, Thomas Adam wrote:
> On 16/04/2008, Ben Okopnik <ben@linuxgazette.net> wrote: > > On Wed, Apr 16, 2008 at 02:07:30PM +0100, Thomas Adam wrote: > > > On 16/04/2008, Francis Daly <francis@daoine.org> wrote: > > > > If you want to replace every instance of a single character with another > > > > single character, "tr" is the tool to use. > > > > > > Not really. As a bashism: > > > > > > `` > > > [thomas@ubuntu ~]$ A="abc,def,qqq,d" > > > [thomas@ubuntu ~]$ echo ${A//,/ } > > > abc def qqq d > > > '' > > > > > > That's the one that came to my mind, right away - but when it comes to > > portability, Francis' method is better. The Bourne shell, or any Bourne > > derivative will handle 'a=`echo $a|tr , ' '` correctly; even CSH > > variants will, once you prefix it with 'set'. But it takes a "modern" > > shell (Bash, KSH, or ZSH) to use the syntax that you suggest. > > Yup. That's why I said "bashism". ;)
I was responding to the "Not really" part.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Ben Okopnik [ben at linuxgazette.net]
On Wed, Apr 16, 2008 at 10:38:08PM +0530, Kapil Hari Paranjape wrote:
> > On Wed, 16 Apr 2008, Ben Okopnik wrote: > > On Wed, Apr 16, 2008 at 02:07:30PM +0100, Thomas Adam wrote: > > > Not really. As a bashism: > > > > > > `` > > > [thomas@ubuntu ~]$ A="abc,def,qqq,d" > > > [thomas@ubuntu ~]$ echo ${A//,/ } > > > abc def qqq d > > > '' > > > > That's the one that came to my mind, right away - but when it comes to > > portability, Francis' method is better. The Bourne shell, or any Bourne > > derivative will handle 'a=`echo $a|tr , ' '` correctly; even CSH > > variants will, once you prefix it with 'set'. But it takes a "modern" > > shell (Bash, KSH, or ZSH) to use the syntax that you suggest. > > The solution using "set --" and IFS works in any posix shell and > does not use any external utilities.
You're right - assuming that there are only two items in the list. For an arbitrary number of items, a 'for' loop would work better:
( # Launch a subshell - the IFS will be reset on exit IFS=, for n in $x; do echo $n; done )
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Amit Kumar Saha [amitsaha.in at gmail.com]
On Wed, Apr 16, 2008 at 4:49 PM, Neil Youngman <ny@youngman.org.uk> wrote:
> On Wednesday 16 April 2008 12:03, Amit Kumar Saha wrote: > > Hello all, > > > > I have got a shell variable (passed as an argument) which stores some > > value such as: > > > > $4 = 'abc,def' > > > > Now, I want to replace all the ',' by a ' ' such that the resulting > > value is 'abc def'. > > > > How do I do it? > > I would do > > var=$(echo $4 | sed -e's/,/ /')
No, it just replaces the first ',' it comes across:
Input -> /dev/sda10,/dev/sda11,/dev/sda9, Output ->/dev/sda10 /dev/sda11,/dev/sda9,
Thanks, Amit
-- Amit Kumar Saha *NetBeans Community Docs Coordinator* http://amitsaha.in.googlepages.com http://amitksaha.blogspot.com
Amit Kumar Saha [amitsaha.in at gmail.com]
On Wed, Apr 16, 2008 at 4:55 PM, Francis Daly <francis@daoine.org> wrote:
> On Wed, Apr 16, 2008 at 04:33:49PM +0530, Amit Kumar Saha wrote: > > Hi there, > > > > I have got a shell variable (passed as an argument) which stores some > > value such as: > > > > $4 = 'abc,def' > > > > Now, I want to replace all the ',' by a ' ' such that the resulting > > value is 'abc def'. > > > > How do I do it? > > > echo $4 > devlist.tmp > > > awk 'BEGIN { FS = "," } ; {print $1, $2 }' devlist.tmp > awk_tmp.tmp > > devs='cat awk_tmp.tmp'; > > echo $devs > > You're nearly there. > > When you set devs=, you want it to be the output the command, so you > must use ` and not ' to surround the command. Or wrap it in $(). > > Note that your current attempt will only print the first two > comma-separated fields -- if your input had been "a,b,c" then your output > would have been "a b". > > And the initial "echo" may have corrupted any whitespace characters in > your input. > > > do suggest a solution! > > If you want to replace every instance of a single character with another > single character, "tr" is the tool to use. > > === > : set the variable > var="abc,def',next more" > : save the change > new=$(echo "$var" | tr , ' ') > > : show the differences > echo :"$var": > echo :"$new": > ===
Works!
Thanks
-- Amit Kumar Saha *NetBeans Community Docs Coordinator* http://amitsaha.in.googlepages.com http://amitksaha.blogspot.com
Neil Youngman [ny at youngman.org.uk]
On Thursday 17 April 2008 06:33, Amit Kumar Saha wrote:
> On Wed, Apr 16, 2008 at 4:49 PM, Neil Youngman <ny@youngman.org.uk> wrote: > > On Wednesday 16 April 2008 12:03, Amit Kumar Saha wrote: > > > Hello all, > > > > > > I have got a shell variable (passed as an argument) which stores some > > > value such as: > > > > > > $4 = 'abc,def' > > > > > > Now, I want to replace all the ',' by a ' ' such that the resulting > > > value is 'abc def'. > > > > > > How do I do it? > > > > I would do > > > > var=$(echo $4 | sed -e's/,/ /') > > No, it just replaces the first ',' it comes across: > > Input -> /dev/sda10,/dev/sda11,/dev/sda9, > Output ->/dev/sda10 /dev/sda11,/dev/sda9,
I forgot the 'g' modifier.
504 $ echo 'a,b,c' | sed -e's/,/ /g' a b c 505 $
Neil
Ben Okopnik [ben at linuxgazette.net]
On Thu, Apr 17, 2008 at 11:03:05AM +0530, Amit Kumar Saha wrote:
> On Wed, Apr 16, 2008 at 4:49 PM, Neil Youngman <ny@youngman.org.uk> wrote: > > On Wednesday 16 April 2008 12:03, Amit Kumar Saha wrote: > > > Hello all, > > > > > > I have got a shell variable (passed as an argument) which stores some > > > value such as: > > > > > > $4 = 'abc,def' > > > > > > Now, I want to replace all the ',' by a ' ' such that the resulting > > > value is 'abc def'. > > > > > > How do I do it? > > > > I would do > > > > var=$(echo $4 | sed -e's/,/ /') > > No, it just replaces the first ',' it comes across: > > Input -> /dev/sda10,/dev/sda11,/dev/sda9, > Output ->/dev/sda10 /dev/sda11,/dev/sda9,
One character more, one character less... I'm sure Neil meant
var=$(echo $4 | sed -e's/,/ /g')
Note the 'g'lobal modifier on the end of the substitution expression.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Amit Kumar Saha [amitsaha.in at gmail.com]
On Fri, Apr 18, 2008 at 1:48 AM, Ben Okopnik <ben@linuxgazette.net> wrote:
> On Thu, Apr 17, 2008 at 11:03:05AM +0530, Amit Kumar Saha wrote: > > On Wed, Apr 16, 2008 at 4:49 PM, Neil Youngman <ny@youngman.org.uk> wrote: > > > On Wednesday 16 April 2008 12:03, Amit Kumar Saha wrote: > > > > Hello all, > > > > > > > > I have got a shell variable (passed as an argument) which stores some > > > > value such as: > > > > > > > > $4 = 'abc,def' > > > > > > > > Now, I want to replace all the ',' by a ' ' such that the resulting > > > > value is 'abc def'. > > > > > > > > How do I do it? > > > > > > I would do > > > > > > var=$(echo $4 | sed -e's/,/ /') > > > > No, it just replaces the first ',' it comes across: > > > > Input -> /dev/sda10,/dev/sda11,/dev/sda9, > > Output ->/dev/sda10 /dev/sda11,/dev/sda9, > > One character more, one character less... I'm sure Neil meant > > `` > var=$(echo $4 | sed -e's/,/ /g') > '' > > Note the 'g'lobal modifier on the end of the substitution expression.
Yes Thanks!
-- Amit Kumar Saha *NetBeans Community Docs Coordinator* http://amitsaha.in.googlepages.com http://amitksaha.blogspot.com