Passing variables and setting them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing variables and setting them
# 1  
Old 04-25-2013
Passing variables and setting them

So I'm writing a script to generate pairwise scores for how similar two strings are, and while I've been able to get it to work on a single script, I've been unable to iterate it.

So suppose I have a file thus
Code:
1234567890
1234567890
1234567899

first I need to assign two lines, by their respective line number, to different variables
set $a=1 and $b=3, so I'm interested in a comparison of the first and third lines
I want to pass that into an awk or sed or something bit that will then assign a new variable to the string of that line
so $j=1234567890 (corresponding to $a) and $k=1234567899 (corresponding to b).
I think want to iterate over the values of those lines, so I need something similar to set another two variables to the character in the same place
so then for $i=1 $m=1 and $n=1
and then I do some boolean comparisons and generate a score.

As I said, I need a line that'll do what I need, but take a variable input. I've tried awk ' NR==...' and sed -n, and a few other things, but they don't pass the variable along. What would you recommend?
# 2  
Old 04-25-2013
Code:
$ cat test.sh
# usage: test.sh line1 line2
a=$1 b=$2

# Method #1

j=`sed -n "$a {p; q}" file`
k=`sed -n "$b {p; q}" file`
score=`expr $j - $k` # You substitute real logic

# Method #2

awk 'NR == a { j = $0 } NR == b { k = $0 } END { score = j - k; print score }' file
file

# 3  
Old 04-25-2013
Quote:
Originally Posted by hanson44
Code:
... ... ...
# Method #2

awk 'NR == a { j = $0 } NR == b { k = $0 } END { score = j - k; print score }' file
file

I think what viored didn't understand was how to get a and b into awk:
Code:
awk -v a="$a" -v b="$b" '
NR == a {j = $0}
NR == b {k = $0}
END { score = ...}' file

If $a and $b haven't already been set when you get to awk you can easily replace that first line with:
Code:
awk -v a=1 -v b=3 '

# 4  
Old 04-25-2013
Yes, of course the variables have to be passed into awk. That's what I get for writing a script without testing.
Code:
# usage: test.sh line1 line2
a=$1 b=$2

# Method #1

j=`sed -n "$a {p; q}" file`
k=`sed -n "$b {p; q}" file`
score=`expr $j - $k` # You substitute real logic

# Method #2

awk 'NR == a { j = $0 } NR == b { k = $0 } END { score = j - k; print score }' a=$a b=$b file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

Passing Variables

I have below data: DAY1=10202013 I am trying below but not getting the desired output: COUNT=1 DATE=DAY$COUNT echo "Date is $DATE" However output I am getting is: Date is DAY1 I wanted the output as: Date is 10202013 I tried following as well: DAY1=10202013 COUNT=1... (3 Replies)
Discussion started by: rockyr1985
3 Replies

3. Shell Programming and Scripting

Passing variables into AWK

I'm trying to use awk to write new entries to a hosts file if they don't exist. I need to do so depending on the type of system I have. Below is what I have, but it isn't working. awk -v myip1=$IP1 myip2=$IP2 myhost1=$HOST1 myhost2=$HOST2' BEGIN { mqhost1=0; mqhost2=0; stap1=0; stap2=0; } ... (4 Replies)
Discussion started by: Boomn4x4
4 Replies

4. Shell Programming and Scripting

Passing 2 variables

Hi All, I need to pass 2 variables name 'vamskt' and 'vamsi'. Here is my question: delete from gpi.usergroup where usg_user_id in ('vamskt'); delete from gpi.userroles where uro_user_id in ('vamskt'); delete from gpi.user where usr_id in ('vamskt'); insert into gpi.user... (3 Replies)
Discussion started by: tvamsikiran
3 Replies

5. Shell Programming and Scripting

Passing string variables

HI all, Very new to shell programming and just wanted some help on how to solve the following problem. I have a small shell script which searches a given file and extracts some string parameters. I want to now be able to call this script from another shell script and somehow pass the parameters... (11 Replies)
Discussion started by: pxy2d1
11 Replies

6. UNIX for Dummies Questions & Answers

Setting up variables

Hi all, I have a shell script that sets up the environment for an application running on UNIX - ksh. This script is run using: . ./script_name XX where XX is a parameter. I want to run it from another shell script but when I do it I don't get the envornment variables set up and the prompt... (3 Replies)
Discussion started by: solar_ext
3 Replies

7. UNIX for Advanced & Expert Users

setting some variables

i have a file .NAMEexport MY_NAME=JOE when i do this at the command prompt #. .NAME $echo MY_NAME $JOEi created a script called Run.sh . .NAME At the command prompt i did #sh Run.sh #echo $MY_NAMEit returns nothing. What have i missed out? (7 Replies)
Discussion started by: new2ss
7 Replies

8. Shell Programming and Scripting

Passing Variables to Awk

Hi I have a unix shell script with an awk statement. I would like to print some of the fields of an input file. However, I would like to print them dynamically, ie by passing the literal $1 $3 into the script to define the output. I have tried the following: variable1='$1' awk... (2 Replies)
Discussion started by: Bab00shka
2 Replies

9. Shell Programming and Scripting

passing variables

Hi, Is there any way to pass variable to a sed script.For awk we have -v option.like that do we have any way to pass variable to a sed script from a awk script or from normal script? Thanx, sounder (1 Reply)
Discussion started by: sounder123
1 Replies

10. Shell Programming and Scripting

Passing Variables to AWK

Does anybody have an explanation for the following: The following scripts runs fine on IRIX64 6.5 but has bugs on Solaris 8. #! /bin/sh echo run only on an SGI machine echo type in linenumber read j echo value read value awk -f rmspass2 level=$value $j'step1.mlf' When the script is... (5 Replies)
Discussion started by: AreaMan
5 Replies
Login or Register to Ask a Question