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
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.
You don't need uniq or nl or a temporary file; see below.
awk 'NF == 5 && x[$NF] == 0 { printf "%d %s\n", ++n, $2 }' |
while read n species
do
printf "Enter vibration amp. for species %s: " "$species"
read amp
sed s/"$species"/"$amp"/ speciesorder.txt
done > "$outfile"
That will give you multiple copies of speciesorder.txt in $outfile.
I also don't see why you need the numbering. You could do:
while read species
do
....
done < file
To prevent multiple copies of the file in $outfile, use the script to write a sed script, then interpret that with sed:
sedscript=$(
while read species
do
printf "Enter vibration amp. for species %s: " "$species"
read amp
printf 's/%s/%s/\n' "$species" "$amp"
done < file
)
sed -e "$sedscript" speciesorder.txt > "$outfile"