Pruning a sentence

Pruning a sentence

am 11.04.2008 14:37:30 von et

Hello,

A string is given as "I am out office"

We are allowed to read any two words from the above string. Assume
that we are reading the two parameters P1 and P2 as "I" and "office"
respectively.

How can we prune "I" and "office" from the given string and store the
output as: "am out"?

TIA,
ET

Re: Pruning a sentence

am 11.04.2008 15:39:23 von PK

ET wrote:

> Hello,
>
> A string is given as "I am out office"
>
> We are allowed to read any two words from the above string. Assume
> that we are reading the two parameters P1 and P2 as "I" and "office"
> respectively.
>
> How can we prune "I" and "office" from the given string and store the
> output as: "am out"?

You should specify how whitespace should be treated in general, and what to
do if a word appears more than once; here I'm assuming that words are
separated by a single space, both before and after the substitution, that
each word appears only once, and that p1 and p2 are distinct. If your
requirements differ, then provide examples and expected outputs.

str="I am out office"
for p1 in I am out office; do
for p2 in I am out office; do
if [ "$p1" != "$p2" ]; then
result=`echo "$str" | \
sed "s/${p1} \?//; s/${p2} \?//; s/ \?$//"`
echo "p1=$p1, p2=$p2, result: --$result--"
fi
done
done

p1=I, p2=am, result: --out office--
p1=I, p2=out, result: --am office--
p1=I, p2=office, result: --am out--
p1=am, p2=I, result: --out office--
p1=am, p2=out, result: --I office--
p1=am, p2=office, result: --I out--
p1=out, p2=I, result: --am office--
p1=out, p2=am, result: --I office--
p1=out, p2=office, result: --I am--
p1=office, p2=I, result: --am out--
p1=office, p2=am, result: --I out--
p1=office, p2=out, result: --I am--

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: Pruning a sentence

am 11.04.2008 15:51:52 von Glenn Jackman

At 2008-04-11 08:37AM, "ET" wrote:
> Hello,
>
> A string is given as "I am out office"
>
> We are allowed to read any two words from the above string. Assume
> that we are reading the two parameters P1 and P2 as "I" and "office"
> respectively.
>
> How can we prune "I" and "office" from the given string and store the
> output as: "am out"?

S='I am out office'
P1='I'
P2='office'
result=''
set -- $S
for word; do
case "$word" in
"$P1"|"$P2") ;;
*) result="$result $word" ;;
esac
done
result="${result# }" ;# trim leading space

--
Glenn Jackman
"If there is anything the nonconformist hates worse than a conformist,
it's another nonconformist who doesn't conform to the prevailing
standard of nonconformity." -- Bill Vaughan

Re: Pruning a sentence

am 11.04.2008 16:03:31 von et

On Apr 11, 6:39=A0pm, pk wrote:
> ET wrote:
> > Hello,
>
> > A string is given as "I am out office"
>
> > We are allowed to read any two words from the above string. =A0Assume
> > that we are reading the two parameters P1 and P2 as "I" and "office"
> > respectively.
>
> > How can we prune "I" and "office" from the given string and store the
> > output as: "am out"?
>
> You should specify how whitespace should be treated in general, and what t=
o
> do if a word appears more than once; here I'm assuming that words are
> separated by a single space, both before and after the substitution, that
> each word appears only once, and that p1 and p2 are distinct. If your
> requirements differ, then provide examples and expected outputs.
>
> str=3D"I am out office"
> for p1 in I am out office; do
> =A0 for p2 in I am out office; do
> =A0 =A0 if [ "$p1" !=3D "$p2" ]; then
> =A0 =A0 =A0 result=3D`echo "$str" | \
> =A0 =A0 =A0 =A0 =A0 =A0 sed "s/${p1} \?//; s/${p2} \?//; s/ \?$//"`
> =A0 =A0 =A0 echo "p1=3D$p1, p2=3D$p2, result: --$result--" =A0 =A0
> =A0 =A0 fi
> =A0 done
> done
>
> p1=3DI, p2=3Dam, result: --out office--
> p1=3DI, p2=3Dout, result: --am office--
> p1=3DI, p2=3Doffice, result: --am out--
> p1=3Dam, p2=3DI, result: --out office--
> p1=3Dam, p2=3Dout, result: --I office--
> p1=3Dam, p2=3Doffice, result: --I out--
> p1=3Dout, p2=3DI, result: --am office--
> p1=3Dout, p2=3Dam, result: --I office--
> p1=3Dout, p2=3Doffice, result: --I am--
> p1=3Doffice, p2=3DI, result: --am out--
> p1=3Doffice, p2=3Dam, result: --I out--
> p1=3Doffice, p2=3Dout, result: --I am--
>
> --
> All the commands are tested with bash and GNU tools, so they may use
> nonstandard features. I try to mention when something is nonstandard (if
> I'm aware of that), but I may miss something. Corrections are welcome.

Thanks for the solution

Re: Pruning a sentence

am 11.04.2008 16:04:18 von et

On Apr 11, 6:51=A0pm, Glenn Jackman wrote:
> At 2008-04-11 08:37AM, "ET" wrote:
>
> > =A0Hello,
>
> > =A0A string is given as "I am out office"
>
> > =A0We are allowed to read any two words from the above string. =A0Assume=

> > =A0that we are reading the two parameters P1 and P2 as "I" and "office"
> > =A0respectively.
>
> > =A0How can we prune "I" and "office" from the given string and store the=

> > =A0output as: "am out"?
>
> =A0 =A0 S=3D'I am out office'
> =A0 =A0 P1=3D'I'
> =A0 =A0 P2=3D'office'
> =A0 =A0 result=3D''
> =A0 =A0 set -- $S
> =A0 =A0 for word; do
> =A0 =A0 =A0 =A0 case "$word" in
> =A0 =A0 =A0 =A0 =A0 =A0 "$P1"|"$P2") ;;
> =A0 =A0 =A0 =A0 =A0 =A0 *) result=3D"$result $word" ;;
> =A0 =A0 =A0 =A0 esac
> =A0 =A0 done
> =A0 =A0 result=3D"${result# }" ;# trim leading space
>
> --
> Glenn Jackman
> =A0 "If there is anything the nonconformist hates worse than a conformist,=

> =A0 =A0it's another nonconformist who doesn't conform to the prevailing
> =A0 =A0standard of nonconformity." -- Bill Vaughan

Glenn,

Can you explain what's going within the case statement?

Regards & TIA,
ET

Re: Pruning a sentence

am 11.04.2008 16:09:30 von rave25

On Apr 11, 8:37 am, ET wrote:
> Hello,
>
> A string is given as "I am out office"
>
> We are allowed to read any two words from the above string. Assume
> that we are reading the two parameters P1 and P2 as "I" and "office"
> respectively.
>
> How can we prune "I" and "office" from the given string and store the
> output as: "am out"?
>
> TIA,
> ET

A small script for you, hope this helps :

echo "Enter Line : "
read line
echo "\nEnter Word 1 : "
read word1
echo "\nEnter Word 2 :"
read word2
echo "\nOUTPUT : "
echo "${line}"|sed 's/'${word1}'//g;s/'${word2}'//g'




output :
=======
Enter Line :
I am out office

Enter Word 1 :
I

Enter Word 2 :
office

OUTPUT :
am out

Re: Pruning a sentence

am 11.04.2008 16:30:16 von et

On Apr 11, 7:09=A0pm, romy wrote:
> On Apr 11, 8:37 am, ET wrote:
>
> > Hello,
>
> > A string is given as "I am out office"
>
> > We are allowed to read any two words from the above string. =A0Assume
> > that we are reading the two parameters P1 and P2 as "I" and "office"
> > respectively.
>
> > How can we prune "I" and "office" from the given string and store the
> > output as: "am out"?
>
> > TIA,
> > ET
>
> A small script for you, hope this helps :
>
> echo "Enter Line : "
> read line
> echo "\nEnter Word 1 : "
> read word1
> echo "\nEnter Word 2 :"
> read word2
> echo "\nOUTPUT : "
> echo "${line}"|sed 's/'${word1}'//g;s/'${word2}'//g'
>
> output :
> =======3D
> Enter Line :
> I am out office
>
> Enter Word 1 :
> I
>
> Enter Word 2 :
> office
>
> OUTPUT :
> =A0am out

Thank You Buddy!

Re: Pruning a sentence

am 11.04.2008 17:00:35 von Joachim Schmitz

ET wrote:
> On Apr 11, 6:51 pm, Glenn Jackman wrote:
>> At 2008-04-11 08:37AM, "ET" wrote:
>>
>>> Hello,
>>
>>> A string is given as "I am out office"
>>
>>> We are allowed to read any two words from the above string. Assume
>>> that we are reading the two parameters P1 and P2 as "I" and "office"
>>> respectively.
>>
>>> How can we prune "I" and "office" from the given string and store
>>> the output as: "am out"?
>>
>> S='I am out office'
>> P1='I'
>> P2='office'
>> result=''
>> set -- $S
>> for word; do
>> case "$word" in
>> "$P1"|"$P2") ;;
>> *) result="$result $word" ;;
>> esac
>> done
>> result="${result# }" ;# trim leading space
>>
> Glenn,
>
> Can you explain what's going within the case statement?
It scans S word wise, discards P1 and P2 (by doing nothing with them) and
puts the rest onto result

Bye, Jojo

Re: Pruning a sentence

am 11.04.2008 18:26:20 von Tyler

On Apr 11, 5:37 am, ET wrote:
> Hello,
>
> A string is given as "I am out office"
>
> We are allowed to read any two words from the above string. Assume
> that we are reading the two parameters P1 and P2 as "I" and "office"
> respectively.
>
> How can we prune "I" and "office" from the given string and store the
> output as: "am out"?
>

Sheesh, all these solutions and no awks ? I have to on merit alone.

s="I am out office"
echo $s | awk '{for(i=1;i $i}}}END{print p}'




T

Re: Pruning a sentence

am 11.04.2008 18:41:05 von Maxwell Lol

ET writes:

> A string is given as "I am out office"

echo "I am out office" | awk '{print $2, $3}'

Re: Pruning a sentence

am 11.04.2008 21:14:21 von cfajohnson

On 2008-04-11, ET wrote:
> Hello,
>
> A string is given as "I am out office"
>
> We are allowed to read any two words from the above string. Assume
> that we are reading the two parameters P1 and P2 as "I" and "office"
> respectively.
>
> How can we prune "I" and "office" from the given string and store the
> output as: "am out"?

If you have it as a string in a variable (string="I am out office"):

set -f
set -- $string
saved="$2 $3"

Or:

temp=${string%* }
saved=${temp# *}


If you are reading the string from a file or other stream:

read a b c d
saved="$b $c"


--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Pruning a sentence

am 11.04.2008 22:35:45 von wayne

Tyler wrote:
> On Apr 11, 5:37 am, ET wrote:
>> Hello,
>>
>> A string is given as "I am out office"
>>
>> We are allowed to read any two words from the above string. Assume
>> that we are reading the two parameters P1 and P2 as "I" and "office"
>> respectively.
>>
>> How can we prune "I" and "office" from the given string and store the
>> output as: "am out"?
>>
>
> Sheesh, all these solutions and no awks ? I have to on merit alone.
>
> s="I am out office"
> echo $s | awk '{for(i=1;i > $i}}}END{print p}'
>
> T

echo 'I am out of the office' |awk '{$1=$NF=""; print}'

Or if the extra space in the front annoys you:

echo 'I am out of the office' |awk '{$1=$NF=""; sub("^ *", ""); print}'

-Wayne

Re: Pruning a sentence

am 12.04.2008 01:26:15 von Glenn Jackman

At 2008-04-11 10:04AM, "ET" wrote:
> On Apr 11, 6:51 pm, Glenn Jackman wrote:
> >
> >     S='I am out office'
> >     P1='I'
> >     P2='office'
> >     result=''
> >     set -- $S
> >     for word; do
> >         case "$word" in
> >             "$P1"|"$P2") ;;
> >             *) result="$result $word" ;;
> >         esac
> >     done
> >     result="${result# }" ;# trim leading space
>
> Can you explain what's going within the case statement?

I'm looping over each space-separated word in the sentence. For each
one, compare it to P1 or P2 ("$P1"|"$P2"). If it matches, do nothing
(i.e. throw it away). In all other cases (the default "*" case), then
the word did not match P1 and it did not match P2, so add it to the
result variable.

--
Glenn Jackman
"If there is anything the nonconformist hates worse than a conformist,
it's another nonconformist who doesn't conform to the prevailing
standard of nonconformity." -- Bill Vaughan

Re: Pruning a sentence

am 13.04.2008 06:43:01 von William James

On Apr 11, 7:37 am, ET wrote:
> Hello,
>
> A string is given as "I am out office"
>
> We are allowed to read any two words from the above string. Assume
> that we are reading the two parameters P1 and P2 as "I" and "office"
> respectively.
>
> How can we prune "I" and "office" from the given string and store the
> output as: "am out"?
>
> TIA,
> ET

ruby -e'puts ("I am out office".split - ARGV).join(" ")' I office

Re: Pruning a sentence

am 14.04.2008 07:19:32 von et

On Apr 13, 9:43=A0am, William James wrote:
> On Apr 11, 7:37 am, ET wrote:
>
> > Hello,
>
> > A string is given as "I am out office"
>
> > We are allowed to read any two words from the above string. =A0Assume
> > that we are reading the two parameters P1 and P2 as "I" and "office"
> > respectively.
>
> > How can we prune "I" and "office" from the given string and store the
> > output as: "am out"?
>
> > TIA,
> > ET
>
> ruby -e'puts ("I am out office".split - ARGV).join(" ")' I office

Thanks a lot guys! All is clear now.

Cheers,
ET