Compare colunmn and find value within a ranges


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare colunmn and find value within a ranges
# 1  
Old 01-13-2016
Compare colunmn and find value within a ranges

Dear All,
sorry for open a new thread but the old one (Find values within range and output) is already marked as resolved but actually it doesn't work properly and the input file are a bit different.

File 1:

Code:
1 195240910 +
2 195240915 -

File2:
Code:
1 195240905 4
1 195240906 4
1 195240907 5
1 195240908 5
1 195240909 3
1 195240910 0
1 195240911 5
1 195240912 5
1 195240913 0
1 195240914 0
1 195240915 3
1 195240916 4
1 195240917 5
1 195240918 8
1 195240919 5
1 195240920 6
2 195240905 7
2 195240906 2
2 195240907 9
2 195240908 9
2 195240909 2
2 195240910 12
2 195240911 2
2 195240912 9
2 195240913 5
2 195240914 9
2 195240915 0
2 195240916 2
2 195240917 9
2 195240918 5
2 195240919 9
2 195240920 6

Well, I would like to compare these two files in this way.
first, column $1 and $2 of both files must match, if so, output the matching values and if column $3 of file 1 is
Code:
+

output the less n value of $3 in File2, otherwise if column $3 of file 1 is
Code:
-

output the more n value of $3 in File2.

so for File1 and File2 output should be (for n=5):

Code:
1 195240910 4 5 5 3 0
2 195240915 9 5 9 2 0

Well, I really hope that color could help to understand.
Any help or suggestion?

Best
# 2  
Old 01-13-2016
Try
Code:
awk '
FNR == NR       {T[$1] = $2
                 S[$1] = sprintf ("%d", $3 N-1)
                 next
                }

NR <= L         {printf "%s%s", $3, L==NR?"\n":" "
                 next
                }

(T[$1] <= $2 + S[$1] || T[$1] == $2 ) &&
T[$1]           {printf "%s %s %s ", $1, T[$1], $3
                 delete T[$1]
                 L = NR + N - 1
                }
' N=5 file1 file2
1 195240910 4 5 5 3 0
2 195240915 0 2 9 5 9

# 3  
Old 01-13-2016
Dear RudiC,
your script show only the last entry.
In fact if File1 is:

Code:
1 195240910 +
1 195240920 +
2 195240915 -

The output is:

Code:
1 195240920 4 5 8 5 6
2 195240915 0 2 9 5 9

instead of:
Code:
1 195240910 4 5 5 3 0
1 195240920 4 5 8 5 6
2 195240915 0 2 9 5 9

file2 is always:
Code:
1 195240905 4
1 195240906 4
1 195240907 5
1 195240908 5
1 195240909 3
1 195240910 0
1 195240911 5
1 195240912 5
1 195240913 0
1 195240914 0
1 195240915 3
1 195240916 4
1 195240917 5
1 195240918 8
1 195240919 5
1 195240920 6
2 195240905 7
2 195240906 2
2 195240907 9
2 195240908 9
2 195240909 2
2 195240910 12
2 195240911 2
2 195240912 9
2 195240913 5
2 195240914 9
2 195240915 0
2 195240916 2
2 195240917 9
2 195240918 5
2 195240919 9
2 195240920 6

Best
# 4  
Old 01-13-2016
The array values for $1 are being overwritten, so the last entry only is valid. You didn't mention there's several $1 values possible.
# 5  
Old 01-13-2016
Yes sorry I didn't, I didn't think about it...
# 6  
Old 01-13-2016
Code:
awk '
{ k=$1 FS $2 }
NR==FNR { a[k]=$3; next }
{ s[NR%n]=$3 }
(k in a) {
  printf "%s %s",$1,$2
  if (a[k]=="+") {
    for (i=1; i<=n; i++) printf " %s",s[(NR+i)%n]
    printf "\n"
  } else { f=n }
}
(f && (f--==1)) {
  for (i=n; i>=1; i--) printf " %s",s[(NR+i)%n]
  printf "\n"
}
' n=5 file1 file2

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 01-13-2016
works good thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files and find missing fields awk

Hello experts! I have 2 files. file1 is a list file containing uniquely names. e.g.: name1 number number name2 number number name5 number number name10 number number ... file2 is a data file arbitrary containing the names of file1 in paragraphs separated by "10" e.g. name4 ... (3 Replies)
Discussion started by: phaethon
3 Replies

2. Shell Programming and Scripting

Find and compare values from different txt files

Hello, i am new in Bash. Actually i have a directory : /home/resultfiles and inside i have these txt files: 531_1.out.res, 531_2.out.res , 531_3.out.res 532_1.out.res, 532_2.out.res , 532_3.out.res 533_1.out.res, 533_2.out.res, 533_3.out.res All these txt files has this format : num_q all... (3 Replies)
Discussion started by: nimpoura
3 Replies

3. Shell Programming and Scripting

Compare and find records of file1 not in file2

hi.. i am using solaris system and ksh and using nawk to get records of file1 not in file2(not line by line comparison). code i am using is nawk 'NR==FNR{a++} !a {print"line:" FNR"->" $0} ' file2 file1 same command with awk runs perfectly on darwin kernel(mac) but in solaris it does line by... (2 Replies)
Discussion started by: Abhiraj Singh
2 Replies

4. Shell Programming and Scripting

Compare the output of find command

Hi All, I am trying to run find command in a script to list out certain files based on a patter. However, when there is no file in the output, the script should exit. Tried a couple of operators (-n, -z) etc but the script does not work. I am confused whether a null string is returned... (3 Replies)
Discussion started by: danish0909
3 Replies

5. Shell Programming and Scripting

To find and compare the data

hi , from the awk command i'm getting the output where the 2nd column of both are match in row wise, if it present in that same column some where else, I cannot get the correct output. awk 'FNR==NR {a=$5;next} $1 in a {if ($5==a) t=0; else {t=$5-a};print $0,t}' file.tsv file2.tsv please... (3 Replies)
Discussion started by: Shenbaga.d
3 Replies

6. Shell Programming and Scripting

Compare two sample files and find common

Hi I have two sample files attached here one file contain entries in one column and second file contains entries in many columns I have to match entries of first file with entries in secon d file form secon column onwards and if matches write "match" in front of it. I tried several... (11 Replies)
Discussion started by: manigrover
11 Replies

7. HP-UX

Compare 2 systems to find any differences

Hi there, I have 2 machines running HP-UX. One off these controllers is able to send mail and the other cannot. I have looked at all the settings that I know and coannot find any differences. Is there a way to audit the 2 machinces by pulling all the settings then compare any differences? ... (2 Replies)
Discussion started by: lodey
2 Replies

8. Shell Programming and Scripting

find command: various date ranges

Hi, I have writtena script that will recursivly go into subdirecotries and report out what files there are in there that have not been accessed over various date ranges. I do this using a number of find commands: find . -path './.snapshot' -prune -o -type f -atime -8 find... (4 Replies)
Discussion started by: littleIdiot
4 Replies

9. Shell Programming and Scripting

script to compare first column of two files and find difference

Hi, I want to write a script which will compare the 1st column of both the files and will give the difference. e.g:- my 1st file contains: 89 /usr 52 /usr/local 36 /tmp 92 /opt 96 /home 27 /etc/opt/EMCom 1 ... (3 Replies)
Discussion started by: adityam
3 Replies
Login or Register to Ask a Question