The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed to replace text with a variable orahi001 UNIX for Dummies Questions & Answers 1 03-06-2008 09:25 PM
replace with value of variable using SED prvnrk Shell Programming and Scripting 2 01-13-2008 11:13 AM
How to replace variable inside the variable mani_um Shell Programming and Scripting 31 08-09-2007 10:56 PM
Force Input in User Defined Variable earnstaf Shell Programming and Scripting 10 06-20-2007 08:35 AM
environment variable is not defined alien12 Linux 0 02-09-2006 11:12 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-13-2007
ce124 ce124 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 1
Replace variable with a 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.

Last edited by ce124; 04-13-2007 at 04:36 AM..
  #2 (permalink)  
Old 04-15-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by ce124
I have a file that has a list of entries in a column

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

You don't need uniq or nl or a temporary file; see below.

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

Code:
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:

Code:
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:

Code:
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"
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0