Lampy | Loans | Loans | Credit Cards | Loans
Shell Scripting [Archive] - ZGeek

PDA

View Full Version : Shell Scripting


minorproblem
08-05-2005, 02:13 PM
Ok i want to remove duplicate letters from a word, that is all and its driving me insane trying to do it. e.g. I have the word "cabbage" i want it to come out as "cabge" if anyone know how to do it rep++ is comming your way, as this is driving me insane!

s3raph
08-05-2005, 03:35 PM
Damn, I don't know how you'd do that in a shell script, the best I could do is tell you to write a program that the shell script would execute.

C0V3R
08-05-2005, 03:52 PM
Can you use perl? If you can it'd be much easier that way.

minorproblem
08-05-2005, 04:16 PM
Nope has to be shell script and i can't write an external program. And dam its got me stumped.

V_Max
08-05-2005, 04:38 PM
Well it's easy in VBScript (http://bricktop.smacktards.org/Mike/Letters/input.htm), this Lye-nucks stuff is too complex for the likes of me ;)

wolfpac181
08-05-2005, 04:41 PM
$sed -e 's/cabbage/cabge/g' thefile.txt > thefile2.txt


:1337:

wolfpac181
08-05-2005, 04:43 PM
This (http://www.watsys.unh.edu/Darlene/TechToolsFiles/LammersUNIXDemo/UNIX_Intro.html) should help with some command stuff.

shell scripts RULE

t101
08-05-2005, 04:47 PM
Im not on a unixish computer right now, but how about seding to replace each character by a it followed by a newline, pipe that to uniq, and then pipe that back to sed to remove all newlines. You might then have to pipe it to echo to put the newline back at the end.

Something like (my seds will be close but wrong):

sed /./.\n/g $1 | uniq | sed /\n//g | echo

minorproblem
08-05-2005, 06:22 PM
Im not on a unixish computer right now, but how about seding to replace each character by a it followed by a newline, pipe that to uniq, and then pipe that back to sed to remove all newlines. You might then have to pipe it to echo to put the newline back at the end.

Something like (my seds will be close but wrong):

sed /./.\n/g $1 | uniq | sed /\n//g | echo
the problem with uniq is that it only removes adjacent lines. And i though about using sort but then that removes the whole meaning of my word in the first place :p

zombie
08-05-2005, 06:30 PM
http://www.regular-expressions.info/duplicatelines.html

t101
08-05-2005, 06:50 PM
Ah, true... hmm.

Well in that case I'd have to default to my perl brain. Below is pseudocode, but I know it can be done in shell:

$ANSWER = '';
for $CHAR [cut -c 1- $1] loop
if grep -v $CHAR $ANSWER
$ANSWER = "$ANSWER$CHAR";
end if
end loop
echo $ANSWER

Or, basically cut the word into individual characters, and then loop through them, only adding them to the $ANSWER if its not already there. Keeps order, solves problem.

I rarely write if/for in shell so not sure of the exact syntax, but it should do it

minorproblem
16-05-2005, 10:29 PM
A few days ago i wrote up a solution figured it would post it for anyone who is interested. Took me a few hours, but i got it.


#!/usr/bin/sh
################################################## #########
# Usage:SubCip.sh <mode><keyword><inputfile> > <outputfile>
# mode :- there are two diffrent modes -e for encryption &
# -d for decryption.
# keyword:- Keyword specifies the keyword for the
# encryption.
################################################## #########

KEYWORD=$2
ALPHABET="abcdefghijklmnopqrstuvwxyz"
CIPHER=`echo $ALPHABET | tr -d "$KEYWORD"`

LENGTH=${#KEYWORD}
while test $LENGTH -ge 0
do
FOO=${KEYWORD:0:1}
KEYWORD=`echo $KEYWORD | tr -d "[$FOO]"`
NEWKEYWORD=$NEWKEYWORD$FOO
let LENGTH=$LENGTH-1
done

CIPHER=$NEWKEYWORD$CIPHER

if test "$1" = '-e'
then
cat $3 | tr "[$ALPHABET]" "[$CIPHER]"
fi

if test "$1" = "-d"
then
echo "Decrypting.."
cat $3 | tr "[$CIPHER]" "[$ALPHABET]"
fi