Replace variable with user defined variable
I have a file that has a list of entries in a column
x z z z x y z The column can have any length and any number of any strings. I need to replace each unique string with a user defined number. I can filter the unique entries out using awk '{if (NF==5) print $2}' file | uniq | nl > list.tmp which would give 1 x 2 y 3 z I then want to ask the user 'what do you want to replace x with? what do you want to replace y with?' and so on. I then set up a loop to read each line in the file as a variable and loop over these and replace each one with the user input Y=1 while [ $Y -le $Nspecies ]; do #Nspecies is 3 in this case species=`grep "$Y" species.tmp | awk '{print $2}'` #pick species in turn echo "Enter vibration amp. for species" $species #user defined replacement read amp sed s/"$species"/"$amp"/ speciesorder.txt >> $outfile #do switch replace Y=$((Y+1)) done This almost works, it loops over the species x,y,z and asks for input but it always overwrites x again and again and doesn't move onto the others. I think the problem lies in the sed substitution of a variable for a user defined variable. I would really appreciate any mistakes you might have spotted or any other ways to do this you might think of. |
here's a starter for 10...
Code:
echo > outfile # truncate it Code:
set -- `sort -u infile` |
Quote:
Quote:
Thanks for trying. Just to clarify. Say I have x z z z x y z I want to replace each occurence of x by 1, y by 2 and z by 3 and so on, whatever the user wants. The file would look like 1 3 3 3 1 2 3 |
I did say they were starting points!
(you are supposed to finish them off) ;) Quote:
this works... </dev/tty read new Quote:
just an exapmple of a slect for you to consider. |
The </dev/tty trick worked but it removed all non-unique entries. Instead of taking
x z z z x y z and replacing each occurence of x by 1, y by 2 and z by 3 and so on, whatever the user wants to give 1 3 3 3 1 2 3 it just gave 1 2 3 I need to replace each unique string with a given number each time that string appears in the input file without losing the order. Back to square 1... |
ok you asked for it ....
usage: script.pl infile > outfile Code:
#! /usr/bin/perl -w |
Spot on! Thankyou so much for your time
|
i want a cut of the profits
;) |
Quote:
|
I modded it a bit
Code:
#! /usr/bin/perl -w outfile automatically becomes replaced.txt but that can easily be fixed. Thanks Billy. I might look into perl a bit more now. |
Good oh,
no probs, it was only a barebones, with no checks or stuff. |
All times are GMT -5. The time now is 09:24 AM. |