Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ha! Who needs sed!

Status
Not open for further replies.
Apr 13, 2004
316
US
Okay, I was bored so I wrote a script that removes double quotes from around words in a text document.

Not hard but I was bored.

#!/usr/dt/bin/dtksh
# removes double quotes around words from a text document.
file=$1
char='"'
rm /tmp/newtext 2>/dev/null
oldword=

while read line
do
set -A word $(echo $line)
numwords=${#word[@]}
i=0
while (( i < numwords )); do
w=${word}; wordlen=${#word}

if [[ $wordlen -eq 1 ]]; then
wordshort=0
else
((wordshort=wordlen-1))
fi

f=${w:0:1}; l=${w:$wordshort}

if [[ $f = $char && $l = $char ]]; then
b=0
e=2
((ws=wordlen-1))
while (( b < ws )); do
if [[ $b -eq 0 ]]; then
d=2
typeset -L$d ch=$w
if [[ $i -eq 0 ]]; then
print -n "" >> /tmp/newtext
else
print -n " " >> /tmp/newtext
fi
else
typeset -L$e ch=$w
typeset -R1 chw=$ch
print -n $chw >> /tmp/newtext
fi
((e+=1))
((b+=1))
done
else
newword=$w
if [[ $newword != $oldword ]]; then
if [[ $i -eq 0 ]]; then
print -n "" >> /tmp/newtext
else
print -n " " >> /tmp/newtext
fi
fi
print -n $w >> /tmp/newtext
oldword=$w
fi
((i+=1))
done
print >> /tmp/newtext
done < $file
 
And this places quotes around words.

Still bored!!

#!/usr/dt/bin/dtksh
# places double quotes around words in a text document.
file=$1
char='"'
match=$2
rm /tmp/newtext 2>/dev/null

while read line; do
set -A word $(echo $line)
numwords=${#word[@]}
i=0
while (( i < numwords )); do
w=${word}; wordlen=${#word}

if [[ $wordlen -eq 1 ]]; then
wordshort=0
else
((wordshort=wordlen-1))
fi

f=${w:0:1}; l=${w:$wordshort}

if [[ $w = $match ]]; then
print -n "$char$match$char" >> /tmp/newtext
else
print -n $w >> /tmp/newtext
fi
((i+=1))
print -n " " >> /tmp/newtext
done
print >> /tmp/newtext
done < $file
 
I think I'd have used sed ;-)
 
If only you could harness your super powers for the purposes of good.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top