Sponsored Content
Top Forums Shell Programming and Scripting Replace variable with a user defined variable Post 302114409 by cfajohnson on Sunday 15th of April 2007 02:56:26 PM
Old 04-15-2007
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"

 

10 More Discussions You Might Find Interesting

1. Linux

environment variable is not defined

moved to correct thread (0 Replies)
Discussion started by: alien12
0 Replies

2. Shell Programming and Scripting

Force Input in User Defined Variable

In a line such as: echo -n "How many days back would you like to check? "; read days How can I ensure that the user has a.) entered a number between 1-30 (not 0 or 31+) and b.) has not just hit enter (ie set it to "") and if it's entered wrong, how do I start the if statement over? I... (10 Replies)
Discussion started by: earnstaf
10 Replies

3. Shell Programming and Scripting

Finding Variable Value defined in a Script

Hi , I had a script in which there is a variable $LOG defined and I want to check where that variable is getting value from, is there a way I can check for that variable and it's value if it is defined in some other script function or script . I checked using command env and also export... (1 Reply)
Discussion started by: somu_june
1 Replies

4. Shell Programming and Scripting

Unable to add user defined variable

Hi, I have a user defined variable _TIME1=xxx I am using awk command for pattern matching. cat $_LOCATION/catalina.txt | awk '/^`$_TIME1`:??:??/' It not taking the value of $_TIME! eg:I am using the command to get all the patter from 12:00:00 to 12:59:59 The user defined variable... (2 Replies)
Discussion started by: ahamed
2 Replies

5. Shell Programming and Scripting

search the pattern in a file and replace with variable already defined previously in csh

I want to replace a certain pattern with the variable already defined. e.g. set path_verilog = /home/priya/bin/verilogfile my file contents are : verilog new verilog is defined here verilog_path_comes I am using the below command sed 's/verilog_path_comes/'$path_verilog'/g' <filename>... (2 Replies)
Discussion started by: nehashine
2 Replies

6. UNIX for Dummies Questions & Answers

Finding WHERE an environment variable is defined.

HI. I had an environment variable defined in my .bash_profile file and I deleted it. I opened a new terminal and looked inside .bash_profile and it's not there but when I type printenv MYCONFIG it reutrns what the value used to be in my .bash_profile still!! I did a grep for MYCONFIG and... (6 Replies)
Discussion started by: dissectcode
6 Replies

7. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

8. Shell Programming and Scripting

Question about awk - create a user-defined variable

Hi, guys, The content of file is below (from <UNIX® Shells by Example Fourth Edition>): My code is below: gawk -F'' ' { OFS = "****"; $3 = "(904)"; $8 = $5 + $6 + $7; print } ' lab3.data The result is below: So, where is the $1? Why is the variable $8 located at the wired position? (3 Replies)
Discussion started by: franksunnn
3 Replies

9. Shell Programming and Scripting

How do I get variable defined in BASH subshell outside?

I'm a BASH shell user (relatively new) I need to get a variable calculated in a subshell, outside the subshell, when it completes. I can do it, by writing the variable into a file, and then reading the file again when outside the subshell. I've tried lots of things from exporting to environmental... (3 Replies)
Discussion started by: goreilly
3 Replies

10. UNIX for Beginners Questions & Answers

Awk: use variable defined in begin

Hi there, I'm working with file more than 400K lines, 60 columns. Column count is going to be multiple of 12: 60, 12, 72 or so. NF/12 gives me on how many iterations I've to do to check certain value. For example: 7, 14th if only 24 columns in file. 7th, 14th and 21st if 36 columns in... (6 Replies)
Discussion started by: genome
6 Replies
DB2_FIELD_PRECISION(3)							 1						    DB2_FIELD_PRECISION(3)

db2_field_precision - Returns the precision of the indicated column in a result set

SYNOPSIS
int db2_field_precision (resource $stmt, mixed $column) DESCRIPTION
Returns the precision of the indicated column in a result set. PARAMETERS
o $stmt - Specifies a statement resource containing a result set. o $column - Specifies the column in the result set. This can either be an integer representing the 0-indexed position of the column, or a string containing the name of the column. RETURN VALUES
Returns an integer containing the precision of the specified column. If the specified column does not exist in the result set, db2_field_precision(3) returns FALSE. SEE ALSO
db2_field_display_size(3), db2_field_name(3), db2_field_num(3), db2_field_scale(3), db2_field_type(3), db2_field_width(3). PHP Documentation Group DB2_FIELD_PRECISION(3)
All times are GMT -4. The time now is 08:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy