Unix Shell - Removing days

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Removing days





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Removing days
apogeusistemas@gmail.com

2007-12-23, 7:31 am

Hi:
I need remove all days from $a variable finded in $b variable, and I
create
this script, but I=B4m having errors...

a=3D"2 3 9 10 16 17 23 24 30 31"
b=3D"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29 30 31"
for day in `echo $a`
do
c=3D`echo $b | tr -d $day`
done
echo $c

=2E/script

2 4 5 6 7 8 9 0 2 4 5 6 7 8 9 20 2 22 2 24 25 26 27 28 29 0

What could I modify to get this script working good ?

Thank You.
Cyrus Kriticos

2007-12-23, 7:31 am

apogeusistemas@gmail.com wrote:
> I need remove all days from $a variable finded in $b variable, and I
> create
> this script, but I´m having errors...
>
> a="2 3 9 10 16 17 23 24 30 31"
> b="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
> 26 27 28 29 30 31"
> for day in `echo $a`
> do
> c=`echo $b | tr -d $day`
> done
> echo $c
>
> ./script
>
> 2 4 5 6 7 8 9 0 2 4 5 6 7 8 9 20 2 22 2 24 25 26 27 28 29 0
>
> What could I modify to get this script working good ?


c=$(echo $a $b | tr " " "\n" | sort -n | sort -u | tr "\n" " ")

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos

2007-12-23, 7:31 am

apogeusistemas@gmail.com wrote:
> I need remove all days from $a variable finded in $b variable, and I
> create
> this script, but I´m having errors...
>
> a="2 3 9 10 16 17 23 24 30 31"
> b="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
> 26 27 28 29 30 31"
> for day in `echo $a`
> do
> c=`echo $b | tr -d $day`
> done
> echo $c
>
> ./script
>
> 2 4 5 6 7 8 9 0 2 4 5 6 7 8 9 20 2 22 2 24 25 26 27 28 29 0
>
> What could I modify to get this script working good ?


c=$(echo $a $b | tr " " "\n" | sort -n | uniq -u | tr "\n" " ")

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
apogeusistemas@gmail.com

2007-12-23, 7:31 am

On 23 dez, 11:11, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
wrote:
> apogeusiste...@gmail.com wrote:
>
>
>
>
>
> c=3D$(echo $a $b | tr " " "\n" | sort -n | uniq -u | tr "\n" " ")
>
> --
> =A0 =A0Best regards =A0| =A0Be nice to America or they'll bring democracy =

to
> =A0 =A0Cyrus =A0 =A0 =A0 =A0 | =A0your country.- Ocultar texto entre aspas=

-
>
> - Mostrar texto entre aspas -


I got this output:

1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30
31 4 5 6 7 8 9
apogeusistemas@gmail.com

2007-12-23, 7:31 am

On 23 dez, 11:11, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
wrote:
> apogeusiste...@gmail.com wrote:
>
>
>
>
>
> c=3D$(echo $a $b | tr " " "\n" | sort -n | uniq -u | tr "\n" " ")
>
> --
> =A0 =A0Best regards =A0| =A0Be nice to America or they'll bring democracy =

to
> =A0 =A0Cyrus =A0 =A0 =A0 =A0 | =A0your country.- Ocultar texto entre aspas=

-
>
> - Mostrar texto entre aspas -



Thanks again, Cyrus !
Cyrus Kriticos

2007-12-23, 7:31 am



apogeusistemas@gmail.com wrote:
> On 23 dez, 11:11, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
> wrote:
>
> I got this output:
>
> 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30
> 31 4 5 6 7 8 9


I got this:

$ a="2 3 9 10 16 17 23 24 30 31"
$ b="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31"
$ c=$(echo $a $b | tr " " "\n" | sort -n | uniq -u | tr "\n" " ")
$ echo $c
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29


and


$ for i in tr sort uniq head bash; do $i --version | head -n 1 ; done
tr (GNU coreutils) 5.97
sort (GNU coreutils) 5.97
uniq (GNU coreutils) 5.97
head (GNU coreutils) 5.97
GNU bash, version 3.2.13(1)-release (i486-pc-linux-gnu)

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
pk

2007-12-23, 1:23 pm

apogeusistemas@gmail.com wrote:

> Hi:
> I need remove all days from $a variable finded in $b variable, and I
> create
> this script, but I´m having errors...
>
> a="2 3 9 10 16 17 23 24 30 31"
> b="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
> 26 27 28 29 30 31"
> for day in `echo $a`
> do
> c=`echo $b | tr -d $day`
> done
> echo $c
>
> ./script
>
> 2 4 5 6 7 8 9 0 2 4 5 6 7 8 9 20 2 22 2 24 25 26 27 28 29 0
>
> What could I modify to get this script working good ?


For example:

a="2 3 9 10 16 17 23 24 30 31"
b="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 \
28 29 30 31"

for day in $a; do
b=`echo "$b" | sed -e "s/\([ ^]\)$day\([ $]\)/\1\2/g"`
done

echo "$b" | tr -s ' '

Ed Morton

2007-12-23, 1:23 pm



On 12/23/2007 6:21 AM, apogeusistemas@gmail.com wrote:
> Hi:
> I need remove all days from $a variable finded in $b variable, and I
> create
> this script, but I=B4m having errors...
>=20
> a=3D"2 3 9 10 16 17 23 24 30 31"
> b=3D"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
> 26 27 28 29 30 31"
> for day in `echo $a`
> do
> c=3D`echo $b | tr -d $day`
> done
> echo $c
>=20
> ./script
>=20
> 2 4 5 6 7 8 9 0 2 4 5 6 7 8 9 20 2 22 2 24 25 26 27 28 29 0
>=20
> What could I modify to get this script working good ?
>=20
> Thank You.


Your script seems to be trying to do the opposite of what you say, so I'm=
not
sure what you really want, but maybe this will give you an idea:

$ a=3D"2 3 9 10 16 17 23 24 30 31"
$ b=3D"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 =
26 27
29 30 31"
$ A=3D`echo "$a" | tr ' ' '\n'`
$ B=3D`echo "$b" | tr ' ' '\n'`
$ C=3D`comm -13 <(echo "$A") <(echo "$B")`
$ c=3D`echo $C`
$ echo "$c"
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29

man comm for details.

Regards,

Ed.

Janis Papanagnou

2007-12-23, 1:23 pm

Ed Morton wrote:
>
> On 12/23/2007 6:21 AM, apogeusistemas@gmail.com wrote:
>
>
>
> Your script seems to be trying to do the opposite of what you say, so I'm not
> sure what you really want, but maybe this will give you an idea:
>
> $ a="2 3 9 10 16 17 23 24 30 31"
> $ b="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
> 29 30 31"
> $ A=`echo "$a" | tr ' ' '\n'`
> $ B=`echo "$b" | tr ' ' '\n'`
> $ C=`comm -13 <(echo "$A") <(echo "$B")`


One could move the tr pipes into the process substitution brackets.

Or - since we use shells that already support process substitution -
even use parameter expansion instead of tr; e.g. in ksh93 or bash3...

C=$( comm -13 <(echo "${a// /$'\n'}") <(echo "${b// /$'\n'}") )


Janis

> $ c=`echo $C`
> $ echo "$c"
> 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29
>
> man comm for details.
>
> Regards,
>
> Ed.
>

Ed Morton

2007-12-23, 1:23 pm



On 12/23/2007 10:50 AM, Janis Papanagnou wrote:
> Ed Morton wrote:
>=20
[vbcol=seagreen]
'm not[vbcol=seagreen]
5 26 27[vbcol=seagreen]
>=20
>=20
> One could move the tr pipes into the process substitution brackets.



Yeah, I know it could be reduced to:

$ a=3D"2 3 9 10 16 17 23 24 30 31"
$ b=3D"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 =
26 27 29
30 31"
$ c=3D`comm -13 <(echo "$A" |tr ' ' '\n') <(echo "$B" |tr ' ' '\n') |tr '=
\n' ' '`
$ echo "$c"
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 29

but I was shooting for clarity....

> Or - since we use shells that already support process substitution -
> even use parameter expansion instead of tr; e.g. in ksh93 or bash3...
>=20
> C=3D$( comm -13 <(echo "${a// /$'\n'}") <(echo "${b// /$'\n'}") )


True. Much prettier.

Ed.

>=20
> Janis
>=20
>=20
>=20


Ed Morton

2007-12-23, 1:23 pm



On 12/23/2007 12:17 PM, Ed Morton wrote:
<snip>

Correction:

> Yeah, I know it could be reduced to:
>
> $ a="2 3 9 10 16 17 23 24 30 31"
> $ b="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 29
> 30 31"
> $ c=`comm -13 <(echo "$A" |tr ' ' '\n') <(echo "$B" |tr ' ' '\n') |tr '\n' ' '`


$ c=`comm -13 <(echo "$a" |tr ' ' '\n') <(echo "$b" |tr ' ' '\n') |tr '\n' ' '`

> $ echo "$c"
> 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 29



Ed.

William James

2007-12-24, 1:39 am

On Dec 23, 7:19 am, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
wrote:
> apogeusiste...@gmail.com wrote:
[vbcol=seagreen]
>
>
>
>
>
> I got this:
>
> $ a=3D"2 3 9 10 16 17 23 24 30 31"
> $ b=3D"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2=

6
> 27 28 29 30 31"
> $ c=3D$(echo $a $b | tr " " "\n" | sort -n | uniq -u | tr "\n" " ")
> $ echo $c
> 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29
>
> and
>
> $ for i in tr sort uniq head bash; do $i --version | head -n 1 ; done
> tr (GNU coreutils) 5.97
> sort (GNU coreutils) 5.97
> uniq (GNU coreutils) 5.97
> head (GNU coreutils) 5.97
> GNU bash, version 3.2.13(1)-release (i486-pc-linux-gnu)


Ruby:

a =3D "2 3 9 10 16 17 23 24 30 31"
b =3D "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31"
puts (b.split - a.split).join(" ")

---- output ----
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29
pk

2007-12-24, 7:33 am

William James wrote:

> Ruby:
>
> a = "2 3 9 10 16 17 23 24 30 31"
> b = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
> 22 23 24 25 26 27 28 29 30 31"
> puts (b.split - a.split).join(" ")
>
> ---- output ----
> 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29


php:

$a = array(2,3,9,10,16,17,23,24,30,31);
$b = range(1,31);
$r = array_diff($a, $b);

Rakesh Sharma

2007-12-24, 7:33 am

You need to re-word your requirements. What you need is to delete the
days from the $b variable not found in the $a variable.
What you wrote is days in $a not to be found in $b, which are none,
since every element of $a variable is present in $b.

### strip any newlines in the $a variable & pad with spaces.
aa=3D" `echo $a` "; # Note: $a variable deliberately left unquoted.
bNOTa=3D
for day in $b; do
case $aa in
*\ "$day"\ * ) :;;
*) bNOTa=3D${bNOTa}${bNOTa:+' '}${day};;
esac
done
echo "\$bNOTa=3D|$bNOTa|"
########################################
#######


On Dec 23, 5:21 pm, apogeusiste...@gmail.com wrote:
> Hi:
> I need remove all days from $a variable finded in $b variable, and I
> create
> this script, but I=B4m having errors...
>
> a=3D"2 3 9 10 16 17 23 24 30 31"
> b=3D"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
> 26 27 28 29 30 31"
> for day in `echo $a`
> do
> c=3D`echo $b | tr -d $day`
> done
> echo $c
>
> ./script
>
> 2 4 5 6 7 8 9 0 2 4 5 6 7 8 9 20 2 22 2 24 25 26 27 28 29 0
>
> What could I modify to get this script working good ?
>
> Thank You.


Cyrus Kriticos

2007-12-24, 7:33 am

pk wrote:
> William James wrote:
>
>
> php:
>
> $a = array(2,3,9,10,16,17,23,24,30,31);
> $b = range(1,31);
> $r = array_diff($a, $b);


Wie sieht der Output aus?

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos

2007-12-24, 7:33 am

pk wrote:
> William James wrote:
>
>
> php:
>
> $a = array(2,3,9,10,16,17,23,24,30,31);
> $b = range(1,31);
> $r = array_diff($a, $b);


And the output?

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
pk

2007-12-24, 7:33 am

Cyrus Kriticos wrote:

>
> And the output?


Change the last line (for example) to:

printf("%s\n", implode(' ', array_diff($b, $a)));

or one of the other bazillions php string/array manipulation methods...

William James

2007-12-24, 1:23 pm

On Dec 24, 4:11 am, pk <p...@pk.pk> wrote:
> William James wrote:
>
>
>
> php:
>
> $a = array(2,3,9,10,16,17,23,24,30,31);
> $b = range(1,31);
> $r = array_diff($a, $b);


You didn't show $a starting as a string, not
an array. What are those silly semicolons for?
Even awk doesn't need semicolons. And this
doesn't produce any output.

a = "2 3 9 10 16 17 23 24 30 31"
puts ( (1..31).to_a - a.split.map{|s| s.to_i} ).join(" ")

a = %w(2 3 9 10 16 17 23 24 30 31)
puts ( (1..31).to_a - a.map{|s| s.to_i} ).join(" ")

a = [2,3,9,10,16,17,23,24,30,31]
puts ( (1..31).to_a - a ).join(" ")

a = 2,3,9,10,16,17,23,24,30,31
puts ( (1..31).to_a - a ).join(' ')

a = 2,3,9,10,16,17,23,24,30,31
puts ( [*(1..31)] - a ).join(' ')

---- output ----
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29
1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29
William James

2007-12-24, 1:23 pm

On Dec 24, 11:08 am, William James <w_a_x_...@yahoo.com> wrote:
> On Dec 24, 4:11 am, pk <p...@pk.pk> wrote:
>
>
>
>
>
>
> You didn't show $a starting as a string, not
> an array. What are those silly semicolons for?
> Even awk doesn't need semicolons. And this
> doesn't produce any output.
>
> a = "2 3 9 10 16 17 23 24 30 31"
> puts ( (1..31).to_a - a.split.map{|s| s.to_i} ).join(" ")
>
> a = %w(2 3 9 10 16 17 23 24 30 31)
> puts ( (1..31).to_a - a.map{|s| s.to_i} ).join(" ")
>
> a = [2,3,9,10,16,17,23,24,30,31]
> puts ( (1..31).to_a - a ).join(" ")
>
> a = 2,3,9,10,16,17,23,24,30,31
> puts ( (1..31).to_a - a ).join(' ')
>
> a = 2,3,9,10,16,17,23,24,30,31
> puts ( [*(1..31)] - a ).join(' ')


Please pardon my prolixity.

a = 2,3,9,10,16,17,23,24,30,31
puts ( [*1..31] - a ).join(' ')
pk

2007-12-24, 1:23 pm

William James wrote:

> You didn't show $a starting as a string, not
> an array.


Not really difficult:

$a = explode(' ', "2 3 9 10 16 17 23 24 30 31");

> What are those silly semicolons for?


It's called "syntax". php statements must be terminated by semicolons.

> Even awk doesn't need semicolons.


Well, in case you don't know, perl, c and a great deal of other languages
do.

> And this doesn't produce any output.


See my other reply; the last line had to be different.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com