A faster way to read and search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A faster way to read and search
# 8  
Old 10-11-2016
You could also try using awk. If the output order doesn't matter, try:
Code:
awk '
FNR == NR {
	k[$0]
	next
}
$0 in k {
	k[$0]++
}
END {	for(key in k)
		if(k[key] > 1)
			print key, k[key]
}' fileA.txt *multfiles.txt

which, with the awk on my system and the sample files you provided, produces the output:
Code:
25.2292,-80.8542,29.1 2
25.2292,-80.8958,29.2 3

but the order of the output lines may vary with different versions of awk.

If the order of the output has to match the order of the entries found in fileA.txt (as in your sample output):
Code:
awk '
FNR == NR {
	k[$0]
	o[++oc] = $0
	next
}
$0 in k {
	k[$0]++
}
END {	for(i = 1; i <= oc; i++)
		if(k[o[i]] > 1)
			print o[i], k[o[i]]
}' fileA.txt *multfiles.txt

which should produce the output:
Code:
25.2292,-80.8958,29.2 3
25.2292,-80.8542,29.1 2

on any system.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.


===========
Sorry, it looks like the 1st suggestion is a late repeat of other suggestions. I won't delete this post in case being able to produce output in the same order as the output produced by the original script in post #1 is desirable.

Last edited by Don Cragun; 10-11-2016 at 03:41 PM.. Reason: Add note.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 10-11-2016
Quote:
Originally Posted by RavinderSingh13
Hello ncwxpanther,

I haven't tested it, could you please change code ext=$(grep "$line" *multfiles.txt | wc -l) with following and let me know if this helps you.
Code:
ext=$(find -type f -exec grep -c "$line" *multfiles.txt)

Thanks,
R. Singh
No this doesn't work, even if \; or + is added, because the output is on multiple lines.
Perhaps a string match (a . is a . not any character) ext=$(fgrep -xhc "$line" *multfiles.txt | wc -l) is more exact (but still slow).
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 10-11-2016
Quote:
Originally Posted by ncwxpanther
Thanks to all that replied!

I was able to get a successful test from a few replies. Below are the times.

Post #3 (using grep) - 17 secs
Post #3 (using awk) - 27 secs
Post #5 - 1.25 secs
Post #6 - 31 secs
Note: if you perform measurements, remember to either clear the cache with each run, or execute the command twice and take the second value, to create similar circumstances and so that the various methods are comparable..
# 11  
Old 10-11-2016
Quote:
Originally Posted by ncwxpanther
Thanks to all that replied!

I was able to get a successful test from a few replies. Below are the times.

Post #3 (using grep) - 17 secs
Post #3 (using awk) - 27 secs
Post #5 - 1.25 secs
Post #6 - 31 secs
I find it hard to believe that #5 and #6 would vary at all, they are essentially the same code. It is possible that #6 was timed first and that caching/buffering allowed #5 to avoid a lot of disk IO.

Edit: Didn't seen Scrutinizer's post before adding this - still worth stating the 5&6 shouldn't vary anywhere near the amount indicated by the testing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need help for faster file read and grep in big files

I have a very big input file <inputFile1.txt> which has list of mobile no inputFile1.txt 3434343 3434323 0970978 85233 ... around 1 million records i have another file as inputFile2.txt which has some log detail big file inputFile2.txt afjhjdhfkjdhfkd df h8983 3434343 | 3483 | myout1 |... (3 Replies)
Discussion started by: reldb
3 Replies

2. Shell Programming and Scripting

Read files incrementally and search for particular string.

Example I have following requirements where i need to search for particular string from the log files.Files will be archived with number attached end to it and creates a new log file. First Day i will ran at 8:00 AM Filename:a.log1 Wed Aug 24 04:46:34... (1 Reply)
Discussion started by: nareshnani211
1 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

Recursive folder search faster than find?

I'm trying to find folders created by a propritary data aquisition software with the .aps ending--yes, I have never encountered folder with a suffix before (some files also end in .aps) and sort them by date. I need the whole path ls -dt "$dataDir"*".aps"does exactly what I want except for the... (2 Replies)
Discussion started by: Michael Stora
2 Replies

5. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

6. Shell Programming and Scripting

Faster search needed

Hope you guys out there can help. I have 2 files as below: file 1: 111,222,333,444,555,666 777,888,999,000,111,222 111,222,333,444,555,888 file 2: 666,AAA 222,BBB 888,CCC I want to get the 6th column from file 1 (example, 666) and check in file 2 for the value in the 2nd column... (9 Replies)
Discussion started by: daytripper1021
9 Replies

7. Shell Programming and Scripting

search pattern and read lines

Hi, I have a huge de-limited file which has pattern : 99"9876"2010-11-21 12:51:01"J"MNOPQRS ID# 2-1234-1234-0099-9876-0 "" <<read>> 99"9876"2010-11-21 12:51:01"K"R-EMP# 01234567 (LOGOFF) "" <<read>> 99"9876"2010-11-21 12:51:01"L" *AUTO LOGOFF* ... (3 Replies)
Discussion started by: angie1234
3 Replies

8. UNIX for Dummies Questions & Answers

Faster than nested while read loops?

Hi experts, I just want to know if there is a better solution to my nested while read loops below: while read line; do while read line2; do while read line3; do echo "$line $line2 $line3" done < file3.txt done < file2.txt done < file1.txt >... (4 Replies)
Discussion started by: chstr_14
4 Replies

9. Shell Programming and Scripting

read lines between search pattern

I have a file split something like 01/11/2010: No of users 100 02/11/2010: No of users 102 03/11/2010: No of users 99 ... I want to search the file for a particular date and then extract the following line with the date, something like 02/11/2010 No of users 102 I can grep... (6 Replies)
Discussion started by: gefa
6 Replies

10. Shell Programming and Scripting

sendmail.cf: How can I read a .db file and search for a token?

Hello, I need to write code in '/etc/mail/sendmail.cf' to verify that a string exists within a hash file ( Such as /etc/mail/key-value.db ). I've searched the web and did find many great articles regarding 'sendmail.cf' however I'm not clear how I can do this specific thing as the online... (0 Replies)
Discussion started by: Devyn
0 Replies
Login or Register to Ask a Question