Can anyone tell me what's wrong with my script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Can anyone tell me what's wrong with my script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 19 December 2014, 12:38 PM EST
Location: Goa / Bangalore, India
Posts: 1,652
Thanks: 82
Thanked 543 Times in 494 Posts
Can anyone tell me what's wrong with my script

Hi...

I am fed up in file handing with array for comparing....

1st I want save first 2 columns of file 1

I tried like this,,


Code:
{getline< "file1";ln[NR]=$1; lt[NR]=$2}

then I read second file's 1st and 2nd column..and saved like this
and small calculation and initialization

Code:
var1 =$1
var2 =$2
calc1= $2-0.1
calc2=$2+0.1
x=0
y=0

for loop for 2nd field of 1st and 2nd column...with a length of file 2's number of lines

Code:
for(j=1;j<=file2's length,j++)

now for loop like this for
Code:
(i=1;i<file1's array length;i++)

comparison

Code:
if lt[i] >=calc1 && lt[i]<=calc2

following if statements
else increment i and again check condition,

once i length reaches file1's array length

print file2's column fields..

then increment of j in 1st for loop

my script is having some mistakes...as I don't know much about either awk or shell...so I could not find where is problem....those who know, please solve my problem


Code:
c=`awk 'END{print NR}'file1`
awk '{{getline< "file1";ln[NR]=$1; lt[NR]=$2}}
FNR<=NR{
l1=$1;
la1=$2;
p=la1+0.1;
m=la1-0.1;
x=0;
y=0;
{
for(j=1;j<='$c';j++)
if(lt[j]>=m && lt[j]<=p)
{
if(l1>=ln[j])
x=x+1
if(l1<=ln[j])
y=y+1
}
}
if(x>=1 && y>=1)
print l1,la1,"Wow you are safe..."
}' OFS="\t" file2.dat

I hope you people understand this thread

Last edited by Akshay Hegde; 02-09-2013 at 09:27 AM.. Reason: need of help.....
Sponsored Links
    #2  
Old 02-09-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 December 2014, 10:44 PM EST
Location: NM
Posts: 10,313
Thanks: 297
Thanked 822 Times in 764 Posts
Instead of showing us an awk script that seems to do things in an odd way -

Please

1. post sample input

2. post expected output
Sponsored Links
    #3  
Old 02-09-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 19 December 2014, 12:38 PM EST
Location: Goa / Bangalore, India
Posts: 1,652
Thanks: 82
Thanked 543 Times in 494 Posts
here I am attaching 2 files needs to be compared...first file2 needs to be compared with file1

condition is mentioned in if statements


Code:
c=`awk 'END{print NR}' file1.txt`
awk '{{getline< "file1.txt";ll[NR]=$1;lt[NR]=$2}}
FNR<=NR{
ll1=$1
lt1=$2
p=$2+0.1
m=$2-0.1
x=0
y=0
ct=ct+1
cc=$3
{
for(j=1;j<='$c';j++)
if(lt[j]>=m && lt[j]<=p)
{
if(ll1>=ll[j])
{x=x+1}
if(ll1<=ll[j])
{y=y+1}
}
}
if(x>=1 && y>=1)
{print ll1,lt1,"You are in"}
}' OFS="\t" file2.txt

this is printing data
but, data is not in file2, it was suppose to print data from file 2 I really don't know what mistake I am doing here....

pls explain if you find my mistake, I guess might be in array

output looks like this


Code:
68.14    23.48    You are in
67.62    23.04    You are in   ------->wrong 23.04 not exist in file2
67.68    23.09    You are in   -------> file1 data is printing
67.73    23.14    You are in   -------> again wrong result...
67.79    23.19    You are in
67.85    23.24    You are in
67.91    23.28    You are in
67.97    23.33    You are in
68.02    23.38    You are in
68.08    23.43    You are in
67.56    22.99    You are in
67.13    22.54    You are in

Attached Files
File Type: txt file1.txt (38.2 KB, 11 views)
File Type: txt file2.txt (2.1 KB, 11 views)

Last edited by Akshay Hegde; 02-09-2013 at 11:27 AM..
    #4  
Old 02-09-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 December 2014, 3:38 AM EST
Location: San Jose, CA, USA
Posts: 5,262
Thanks: 206
Thanked 1,756 Times in 1,496 Posts
Quote:
Originally Posted by Akshay Hegde View Post
here I am attaching 2 files needs to be compared...first file2 needs to be compared with file1

condition is mentioned in if statements


Code:
c=`awk 'END{print NR}' file1.txt`
awk '{{getline< "file1.txt";ll[NR]=$1;lt[NR]=$2}}
FNR<=NR{
ll1=$1
lt1=$2
p=$2+0.1
m=$2-0.1
x=0
y=0
ct=ct+1
cc=$3
{
for(j=1;j<='$c';j++)
if(lt[j]>=m && lt[j]<=p)
{
if(ll1>=ll[j])
{x=x+1}
if(ll1<=ll[j])
{y=y+1}
}
}
if(x>=1 && y>=1)
{print ll1,lt1,"You are in"}
}' OFS="\t" file2.txt

this is printing data
but, data is not in file2, it was suppose to print data from file 2 I really don't know what mistake I am doing here....

pls explain if you find my mistake, I guess might be in array

output looks like this


Code:
68.14    23.48    You are in
67.62    23.04    You are in   ------->wrong 23.04 not exist in file2
67.68    23.09    You are in   -------> file1 data is printing
67.73    23.14    You are in   -------> again wrong result...
67.79    23.19    You are in
67.85    23.24    You are in
67.91    23.28    You are in
67.97    23.33    You are in
68.02    23.38    You are in
68.08    23.43    You are in
67.56    22.99    You are in
67.13    22.54    You are in

I agree with Jim. You make us download two files to see that you have one file with spaces as field separators and another file with a tab as the field separator. You give us some awk code that contains no comments and does not do what you want it to do. You tell us that the comments in the if statements explain what you're trying to do (but as I said, there are no comments in your code). You ask us to guess at what you mean by "2 files need to be compared". We can't read your code and tell you what is wrong with it if we don't know what you're trying to do.

Please tell us what you are trying to do in English. Show us sample input and show us the output you want from that sample input. Don't show us that your code doesn't give you what you want; show us what you do want.
Sponsored Links
    #5  
Old 02-09-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 19 December 2014, 12:38 PM EST
Location: Goa / Bangalore, India
Posts: 1,652
Thanks: 82
Thanked 543 Times in 494 Posts
Hammer & Screwdriver Logic I want to use in awk script


Code:
I just want to use this logic in shell awk script

1 . First read column 1 and column 2 of file1.txt store it in array(reference)

2 . Now read column 1 and column 2 of file2.txt store it in another array,to be compared with reference 

3 . for loop : - for(i=1;i<=file2.txt_length;i++)

4 . x=0; y=0 this will get clear as and when i will increment

5 . nested loop :-  for(j=1;j<=file1.txt_lenght;j++)

6 . if(col1_f1[j]>=col1_f2-0.1[i] && col1_f1[j]<=col1_f2+0.1[i])

if above if statement is true then,go to following statement else check condition with j++

7 . if(col2_f2[i]>=col2_f1[j]) then x=x+1

8 . if(col2_f2[i]<=col2_f1[j]) then y=y+1

repeat 5,6,7 and 8 till j reaches file1.txt_length(NR),Once j reaches NR following

9 . if(x>=1 && y>=1) then print column1_f2[i] column2_f2[i] 

repeat statements after number 4 to 9 till i reaches file2.txt_length(NR)

I hope this will be helpful

this logic is to find whether file 2.txt is falling within the range of file1.txt(reference)
if falling, how many records are near to records of file1.txt

and this is for mapping purpose, one can see script's result preview in GMT, with reference file

I am trying to write the same logic in awk script, but I couldn't succeed..those who know...awk very well pls help

Thanks in advance.........................

Last edited by Akshay Hegde; 02-10-2013 at 09:53 AM.. Reason: ???? Awaiting for reply......
Sponsored Links
    #6  
Old 02-13-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 December 2014, 3:38 AM EST
Location: San Jose, CA, USA
Posts: 5,262
Thanks: 206
Thanked 1,756 Times in 1,496 Posts
Quote:
Originally Posted by Akshay Hegde View Post
Code:
I just want to use this logic in shell awk script

1 . First read column 1 and column 2 of file1.txt store it in array(reference)

2 . Now read column 1 and column 2 of file2.txt store it in another array,to be compared with reference 

3 . for loop : - for(i=1;i<=file2.txt_length;i++)

4 . x=0; y=0 this will get clear as and when i will increment

5 . nested loop :-  for(j=1;j<=file1.txt_lenght;j++)

6 . if(col1_f1[j]>=col1_f2-0.1[i] && col1_f1[j]<=col1_f2+0.1[i])

if above if statement is true then,go to following statement else check condition with j++

7 . if(col2_f2[i]>=col2_f1[j]) then x=x+1

8 . if(col2_f2[i]<=col2_f1[j]) then y=y+1

repeat 5,6,7 and 8 till j reaches file1.txt_length(NR),Once j reaches NR following

9 . if(x>=1 && y>=1) then print column1_f2[i] column2_f2[i] 

repeat statements after number 4 to 9 till i reaches file2.txt_length(NR)

I hope this will be helpful

this logic is to find whether file 2.txt is falling within the range of file1.txt(reference)
if falling, how many records are near to records of file1.txt

and this is for mapping purpose, one can see script's result preview in GMT, with reference file

I am trying to write the same logic in awk script, but I couldn't succeed..those who know...awk very well pls help

Thanks in advance.........................
Even with all of your color coding, this logic isn't clear to me. But, just looking at the 1st and last line of your 2nd awk script:

Code:
awk '{{getline< "file1.txt";ll[NR]=$1;lt[NR]=$2}}
}' OFS="\t" file2.txt

we can see that for every line you read from file2.txt , you replace $1 and $2 with a line from file1.txt and since file1.txt has many more lines than file2.txt , you are not reading all of file1.txt and you are not saving anything at all read from file2.txt .

The following awk program will perform steps 1 and 2 in your list above. You can build the rest of your steps into the END clause where I currently have the printf statement that shows that there are 2442 lines in your file1.txt and 217 lines in your file2.txt file.

Code:
awk '
FNR == NR {
        # Save a line from the 1st input file...
        col1_f1[++f1c] = $1
        col2_f1[f1c] = $2
        next
}
{       # Save a line from the 2nd input file...
        col1_f2[++f2c] = $1
        col2_f2[f2c] = $2
}
END {   # Do whatever processing you want to do with the f1c lines you have
        # read into col1_f1[] and col2_f1[] and with the f2c lines you have
        # read into col1_f2[] and col2_f2[].
        # ... ... ...
        printf("%d lines in 1st input file, %d lines in 2nd file.\n", f1c, f2c)
}' file1.txt file2.txt

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What's wrong on this script? kotsos13 UNIX for Dummies Questions & Answers 7 12-19-2011 06:57 PM
What is wrong with this script? heprox Shell Programming and Scripting 8 11-16-2006 05:43 AM
something wrong with this script?? fusion76 Shell Programming and Scripting 2 02-01-2005 01:24 AM
What is wrong with this script? borncrazy Shell Programming and Scripting 5 07-20-2004 11:51 AM
What is wrong with my script? Lem2003 UNIX for Dummies Questions & Answers 6 05-29-2003 02:17 AM



All times are GMT -4. The time now is 05:14 AM.