Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching for strings stored in a file 1

Status
Not open for further replies.

lbzh

Programmer
Aug 7, 2004
25
US
I have a list of account numbers stored in file1.

I would like to search for those account numbers in File2,
when it is found I would like to print out the entire line of File2. The account number appears in the first 11 positions in file 2.

Any ideas on how to accomplish this?

Thanks
 
a sample of File1 and File2 would help.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Sorry, here are the file formats:

File1
12345678901
23456789012


File2

12345678901ABCDEFGHIJ
23456789012DEFGHIJKLM
99999999999KLDSDDSDED

The account number is the only entry in File1, and the account number is in the beginning of File2.

Thanks
 
nawk -f lb.awk File1 File2

here's the lb.awk

Code:
NR == FNR { arr[$0]; next }
substr($0,1,11) in arr

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I ran it as suggested above (using the exact syntax)and here is the error I got:

nawk: syntax error at source line 1
context is
<<< NR == FNR { arr[$0]; next >>> }
nawk: bailing out at source line 1

Any ideas?

Thanks
 
nope, no idea - works just fine on Solaris8.
make sure your awk script is as posted.

maybe your 'nawk' is linked to something else (awk).

try the following:
/usr/xpg4/bin/awk -f lb.awk File1 File2

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I got this error now:
/usr/xpg4/bin/awk: file "acc.awk": line 1: invalid character "^M" Context is:
>>> NR == FNR { arr[$0]; next }^M <<<


Here is my exact code from the calling script:
/usr/xpg4/bin/awk -f acc.awk accounts.txt target2.txt

and acc.awk:

NR == FNR { arr[$0]; next }
substr($0,1,11) in arr


Note: accounts.txt has the 11 digit account and target2.txt
has the account# in the first 11 places and additional fields afterwards.
Thanks
 
make sure you have no ^M (CTRL-M) characters in your file(s). You have probably transfered files to/from your PC in binary more. If that's the case ftp them in ASCII mode.

there's nothing wrong with the script I've posted.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You were right, I copied it from PC and introduced the carriage control. Once corrected it works like a charm.

Thanks very much for your help!!!!!!!!!!!!!!

Do you know a good nawk tutorial so I can understand this solution better?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top