LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-13-2007, 03:39 AM   #1
ce124
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Rep: Reputation: 0
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.
 
Old 04-13-2007, 04:00 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
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
 
Old 04-13-2007, 04:22 AM   #3
ce124
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 04-13-2007, 04:47 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
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.
 
Old 04-13-2007, 05:44 AM   #5
ce124
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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...
 
Old 04-13-2007, 06:06 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
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;

Last edited by bigearsbilly; 04-13-2007 at 06:08 AM.
 
Old 04-13-2007, 07:46 AM   #7
ce124
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Spot on! Thankyou so much for your time
 
Old 04-13-2007, 07:56 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
i want a cut of the profits

 
Old 04-13-2007, 07:57 AM   #9
ce124
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 04-13-2007, 08:55 AM   #10
ce124
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 04-13-2007, 09:29 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
Good oh,
no probs, it was only a barebones, with no checks or stuff.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM
# ./startup.sh Neither the JAVA_HOME nor the JRE_HOME environment variable is defined Niceman2005 Linux - Newbie 3 06-29-2005 01:58 PM
diff between #define and const defined variable b123coder Programming 7 06-27-2005 08:02 AM
Find variable in template file replace w/date+ and save as jmanjohn61 Linux - General 14 12-13-2004 06:49 AM
bash script to replace char in variable gmartin Programming 5 11-08-2004 01:46 PM


All times are GMT -5. The time now is 05:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration