match numbers (awk)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting match numbers (awk)
# 1  
Old 03-09-2005
match numbers (awk)

i would like to enter (user input) a bunch of numbers seperated by space:
10 15 20 25
and use awk to print out any lines in a file that have matching numbers
so output is:

22 44 66 55 (10) 77 (20)

(numbers 10 and 20 matched for example)

is this possible in awk . im using gawk for windows

thanks
# 2  
Old 03-09-2005
On a UNIX system you can do something like this - your Windows milage may vary...

Code:
#!/usr/bin/awk -f

BEGIN {
   # This "getline" reads from STDIN - terminate with Return then EOF
   getline input
}

END {
   # Change "./mymatches" to the path to the file containing the matches
   # The match file should have the numbers, space seperated, on ONE line
   getline line < "./mymatches"
   input_count = split( input, input_array, " ");
   line_count = split( line, line_array, " ");
   for ( i = 1; i <= input_count; i++ ) {
      for ( j = 1; j <= line_count; j++ ) {
         if ( input_array[ i ] == line_array[ j ] ) {
             printf( "%s matches\n", input_array[ i ] )
         }
      }
   }
}

Run on a UNIX machine, the session proceeds as follows....
Code:
$ ./my.awk
10 15 20 25 <return>
<EOF>
10 matches
20 matches
$

The EOF character on a UNIX machine is produced by typing Ctrl-D - on Windows it's Ctrl-Z.

Cheers
ZB
# 3  
Old 03-09-2005
thanks for your reply zazzybob, it almost worked

i have these numbers in mymatches file:
22 44 66 55 10 77 20
23 45 67 56 11 13 25

i enter 10 15 20 25
it outputs
^Z
10 matches
20 matches

it works for the first line but doesnt seem to read next line (nubmer 25 match)

i would like the output to show the numbers that don't match also.

for example, i would like this output if possible
22 44 66 55 (10) 77 (20)
23 45 67 56 11 13 (25)

i tried to modify the awk program you submitted (i have zero experience programming)

Code:
#!/usr/bin/awk -f

BEGIN {
   # This "getline" reads from STDIN - terminate with Return then EOF
   getline input
}

END {
   # Change "./mymatches" to the path to the file containing the matches
   # The match file should have the numbers, space seperated, on ONE line
   getline line < "./mymatches"
   input_count = split( input, input_array, " ");
   line_count = split( line, line_array, " ");
   for ( i = 1; i <= input_count; i++ ) {
      for ( j = 1; j <= line_count; j++ ) {
         if ( input_array[ i ] == line_array[ j ] ) {
#           printf( "%s matches\n", input_array[ i ] )
            printf( "(%s) ", input_array[ i ] )
         }
	 else printf( "%s ", line_array[j])
      }
   }
}

this is the output:
22 44 66 55 (10) 77 20 22 44 66 55 10 77 20 22 44 66 55 10 77 (20) 22 44 66 55 10 77 20
# 4  
Old 03-09-2005
I have changed the script to allow for more than one line in the "mymatches" file... give this a try
Code:
#!/usr/bin/awk -f

BEGIN {
   getline input
}

END {
   while ( ( getline readfile < "./mymatches" ) > 0 ) {
      line = line" "readfile
   }
   input_count = split( input, input_array, " ");
   line_count = split( line, line_array, " ");
   for ( j = 1; j <= line_count; j++ ) {
      flag_2 = 0
      for ( i = 1; i <= input_count; i++ ) {
         if ( input_array[ i ] == line_array[ j ] ) {
             printf( "(%s) ", line_array[ j ] )
             flag = 1
             flag_2 = 1
         } else {
             flag = 0
         }
      }
      if ( flag == 0 && flag_2 == 0 ) {
         printf( "%s ", line_array[ j ] )
      }
   }
   printf( "\n" )
}

Here's the session....
Code:
$ ./my.awk
10 15 20 25
^D
22 44 66 55 (10) 77 (20) 23 45 67 56 11 13 (25)
$

Cheers
ZB
# 5  
Old 03-09-2005
or maybe this:

tanku.awk ./mymatches

here's tanku.awk
Code:
#!/bin/nawk -f
#
BEGIN {
  printf("Input your numbers-> ")
  getline line < "-"
  nl=split(line, lineA, FS)
  for(i=1; i <= nl; i++)
    if ( i != lineA[i]) {
       lineA[lineA[i]]=i
       delete lineA[i]
    }
}

{
   for(i=1; i<=NF; i++)
      if ( $i in lineA)
         $i="(" $i ")"
   print
}


Last edited by vgersh99; 03-09-2005 at 07:32 PM..
# 6  
Old 03-09-2005
@zazzybob
that worked great... your original program worked 100% for one line (sorry, didn't carefully read your comment).
i would like to insert print \n somewhere so the output is on seperate lines similar to mymatches. any ideas

@vgersh99
thank you for tanku.awk. it worked perfectly! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Dummies Questions & Answers

Determine if first 2 digits of string match numbers

Trying to find out how to discover if the first 2 characters of a string are "22" Not sure how. I could use if ]; then echo "yes";fi But I think that will only grab the pattern 22 and not the first 2 digits. (5 Replies)
Discussion started by: newbie2010
5 Replies

4. Shell Programming and Scripting

Match on a range of numbers

Hi, I'm trying to match a filename that could be called anything from vout001 to vout252 and was trying to do a small test but I'm not getting the result I thought I would.. Can some one tell me what I'm doing wrong? *****@********>echo $mynumber ... (4 Replies)
Discussion started by: Jazmania
4 Replies

5. Shell Programming and Scripting

awk : match only the pattern string , not letters or numbers after that.

Hi Experts, I am finding difficulty to get exact match: file OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE="" DHCP_ENABLE=0 INTERFACE_NAME="lan3:1"... (6 Replies)
Discussion started by: rveri
6 Replies

6. Shell Programming and Scripting

Match pattern and get lines numbers in a vector

Hi everyone, I am new to shell scripting, and would appreciate your help on following problem. I need to search a file for a pattern, then get the number of each line that matches the given pattern. Then I need to search those specific line numbers from the first file in a second file and... (6 Replies)
Discussion started by: dimcick
6 Replies

7. Shell Programming and Scripting

Complex match of numbers between 2 files awk script

Hello to all, I hope some awk guru could help me. I have 2 input files: File1: Is the complete database File2: Contains some numbers which I want to compare File1: "NUMBERKEY","SERVICENAME","PARAMETERNAME","PARAMETERVALUE","ALTERNATENUMBERKEY"... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

8. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

9. Shell Programming and Scripting

Match real numbers in AWK

I am looking for a better way to match real numbers within a specified tolerance range. My current code is as follows: if ($1 !~ /^CASE/) for(i=1;i in G;i++) if (G >= $5-1 && G <= $5+1) { print $1,$4,$5,J,G } else { print $1,"NO MATCH" } where $5 and G are... (3 Replies)
Discussion started by: cold_Que
3 Replies

10. Shell Programming and Scripting

match range of different numbers by AWK

if the column1 and 2 in both files has same key (for example "a" and "a1") compare each first key value(a1 of a) of input2 (for example 1-4 or 65-69 not 70-100 or 44-40 etc) with all the values in input1. if the range of first key value in input2 is outof range in input1 values named it as out... (54 Replies)
Discussion started by: repinementer
54 Replies
Login or Register to Ask a Question