LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Replace variable with user defined variable (https://www.linuxquestions.org/questions/programming-9/replace-variable-with-user-defined-variable-545695/)

ce124 04-13-2007 03:39 AM

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.

bigearsbilly 04-13-2007 04:00 AM

here's a starter for 10...
Code:

echo > outfile # truncate it

sort -u infile |
while read input ;do
  echo replace $input with?
  read new
  echo $new >> outfile
done

or maybe...
Code:

set -- `sort -u infile`
select input in $*
do
  echo change to?
  read change
  echo $input = $change >> outfile
done


ce124 04-13-2007 04:22 AM

Quote:

Originally Posted by bigearsbilly
Code:

echo > outfile # truncate it

sort -u infile |
while read input ;do
  echo replace $input with?
  read new
  echo $new >> outfile
done


Afraid this doesn't work. It doesn't pause to let me enter the variable it just replaces the first line with a blank.

Quote:

Code:

set -- `sort -u infile`
select input in $*
do
  echo change to?
  read change
  echo $input = $change >> outfile
done


This one put each unique entry on a numbered line and then endlessly asks for #?. Doesn't matter what I enter it cycles over and over.

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

bigearsbilly 04-13-2007 04:47 AM

I did say they were starting points!
(you are supposed to finish them off)
;)



Quote:

Afraid this doesn't work. It doesn't pause to let me enter the variable it just replaces the first line with a blank.
DOH! I'll give you that one though
this works...
</dev/tty read new

Quote:

Doesn't matter what I enter it cycles over and over.
er, press cntrl-C to quit it
just an exapmple of a slect for you to consider.

ce124 04-13-2007 05:44 AM

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...

bigearsbilly 04-13-2007 06:06 AM

ok you asked for it ....



usage: script.pl infile > outfile
Code:

#! /usr/bin/perl -w
use strict;
use vars qw($old $new $slurp);

open IN, "sort -u @ARGV|" or die "oops:$!";

{
    local $/ = undef;
    $slurp = <ARGV>;
}

foreach $old (<IN>) {
    print "\nchange $old to:";
    $new = <STDIN>;
    $slurp =~ s/$old/$new/g;
}

print $slurp;


ce124 04-13-2007 07:46 AM

Spot on! Thankyou so much for your time

bigearsbilly 04-13-2007 07:56 AM

i want a cut of the profits

;)

ce124 04-13-2007 07:57 AM

Quote:

Originally Posted by ce124
Spot on!

Not quite. It lets me specify the changes for each unique entry and it asks for one for the blank lines even if I strip these out.

ce124 04-13-2007 08:55 AM

I modded it a bit

Code:

#! /usr/bin/perl -w
use strict;
use vars qw($old $new $slurp);

open IN, "sort -u @ARGV|" or die "oops:$!";

{
    local $/ = undef;
    $slurp = <ARGV>;
}

foreach $old (<IN>) {
    print "\nSpecify vibrational amplitude for species $old";
    $new = <STDIN>;
    $slurp =~ s/$old/$new/g;
}

open(INFO, ">replaced.txt");
print INFO $slurp;
close(INFO);

usage script.pl infile

outfile automatically becomes replaced.txt but that can easily be fixed.

Thanks Billy. I might look into perl a bit more now.

bigearsbilly 04-13-2007 09:29 AM

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.