Tux

...making Linux just a little more fun!

Shell scripting Help

Amit Kumar Saha [amitsaha.in at gmail.com]


Wed, 16 Apr 2008 16:33:49 +0530

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


Top    Back


Neil Youngman [ny at youngman.org.uk]


Wed, 16 Apr 2008 12:19:09 +0100

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


Top    Back


Francis Daly [francis at daoine.org]


Wed, 16 Apr 2008 12:25:51 +0100

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        [email protected]


Top    Back


Kapil Hari Paranjape [kapil at imsc.res.in]


Wed, 16 Apr 2008 17:07:42 +0530

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


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Wed, 16 Apr 2008 14:07:30 +0100

On 16/04/2008, Francis Daly <[email protected]> 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


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Wed, 16 Apr 2008 12:05:33 -0400

On Wed, Apr 16, 2008 at 02:07:30PM +0100, Thomas Adam wrote:

> On 16/04/2008, Francis Daly <[email protected]> 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 *


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Wed, 16 Apr 2008 17:31:34 +0100

On 16/04/2008, Ben Okopnik <[email protected]> wrote:

> On Wed, Apr 16, 2008 at 02:07:30PM +0100, Thomas Adam wrote:
>  > On 16/04/2008, Francis Daly <[email protected]> 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


Top    Back


Kapil Hari Paranjape [kapil at imsc.res.in]


Wed, 16 Apr 2008 22:38:08 +0530

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.


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Wed, 16 Apr 2008 13:24:22 -0400

On Wed, Apr 16, 2008 at 05:31:34PM +0100, Thomas Adam wrote:

> On 16/04/2008, Ben Okopnik <[email protected]> wrote:
> > On Wed, Apr 16, 2008 at 02:07:30PM +0100, Thomas Adam wrote:
> >  > On 16/04/2008, Francis Daly <[email protected]> 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 *


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Wed, 16 Apr 2008 20:11:40 -0400

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 *


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Thu, 17 Apr 2008 11:03:05 +0530

On Wed, Apr 16, 2008 at 4:49 PM, Neil Youngman <[email protected]> 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


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Thu, 17 Apr 2008 11:26:30 +0530

On Wed, Apr 16, 2008 at 4:55 PM, Francis Daly <[email protected]> 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


Top    Back


Neil Youngman [ny at youngman.org.uk]


Thu, 17 Apr 2008 08:03:33 +0100

On Thursday 17 April 2008 06:33, Amit Kumar Saha wrote:

> On Wed, Apr 16, 2008 at 4:49 PM, Neil Youngman <[email protected]> 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


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Thu, 17 Apr 2008 16:18:28 -0400

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 <[email protected]> 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 *


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Fri, 18 Apr 2008 10:33:33 +0530

On Fri, Apr 18, 2008 at 1:48 AM, Ben Okopnik <[email protected]> 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 <[email protected]> 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


Top    Back