Using grep and a parameter file to return unique values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep and a parameter file to return unique values
# 8  
Old 03-20-2014
Sorry clippertm,
I made a mistake, delete the double plus.
Code:
grep -h '4=[745-755]' *.* | grep '3078=' | sort -u -t'|' -k2,2

?
If [745-755] is not available, use this:
Code:
grep -h '4=\(74[5-9]\|75[0-5]\)' a | grep '3078=' | sort -u -t'|' -k2,2

This User Gave Thanks to Lucas_0418 For This Post:
# 9  
Old 03-20-2014
Hi Lucas,

No problem.

Thanks, the count loop seems to work, but the whole command does not seem to return unique occurences anymore, but all of them Smilie
# 10  
Old 03-20-2014
Your first problem is that you need to match the patterns in your pattern file with the second field. You cannot just use grep -f list.txt. You really need to use awk or perl. You can then pattern-match on the two fields you are interested in without a stray 254=47587 in, say, the last field, matching your patterns file.

Something I was not sure about. Are you after the first instance of 4=475 and 3078= AND the first instance of 4=476 and 3078= etc etc rather than the first instance of ANY 4=xxx and 3078=? In which case you will have to loop through your patterns file anyway.

I am not going to offer any code because I don't really know awk and my perl is rusty. Good luck

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 11  
Old 03-20-2014
Hi Apm,

Well thank you for your reply, but I only know grep Smilie

I have never used awk or perl Smilie

---------- Post updated at 04:28 AM ---------- Previous update was at 04:24 AM ----------

I have come up with this:

Quote:
grep '4=[0-1][0-9][0-9][0-9]' *.* | grep '3078=' | sort -u -t'|' -k 2,2
However it still returns all the occurrences, not only the first one

---------- Post updated at 04:39 AM ---------- Previous update was at 04:28 AM ----------

Actually, it seems that the simple:
Quote:
grep -h -m 1 '4=[1-2000]' *.* | grep '3078=' > output.txt
does the trick!

Really simple.. am I missing anything?
# 12  
Old 03-20-2014
Sorry clippertm,
I think the problem of output all occurences is that we used the wildcard *.*
so maybe we must use sort after grep
Or use perl or awk as apmcd47 say, u know, both could solve the problem.Smilie

We know the wildcard *.* make the grep consider every first match in every file both are first occurence, may cat *.*|grep could work, but I am not able to test it when I am in a bus.

And sorry for my not very good English, let me check what's your desired output again.
a. line has 4=745 and 3078=
b. line only has 4=475 not 3078=
c. line only has 3078= not 4=475

For your addtional question:
\([1-9]\|[1-9][0-9]\|[1-9][0-9][0-9]\|1[0-9][0-9][0-9]\|2000\)

Last edited by Lucas_0418; 03-20-2014 at 07:03 AM..
This User Gave Thanks to Lucas_0418 For This Post:
# 13  
Old 03-20-2014
Nope it does not work
Quote:
grep -h -m 1 '4=[1-2000]' *.* | grep '3078=' > output.txt
returns less lines than it should...

---------- Post updated at 04:44 AM ---------- Previous update was at 04:43 AM ----------

Hi Lucas,

I do not how to use awk/perl at all Smilie

---------- Post updated at 04:54 AM ---------- Previous update was at 04:44 AM ----------

Does anybody know how to check a range from 1 to 2000? Not 0001, 0002, 0003 etc. but 1, 2, 3
# 14  
Old 03-20-2014
With awk:
Code:
awk -F "|" '
# hash the search list
NR==FNR {L[$1]=0; next}
# now procede with the data files
# print if the following is true
($4~/^3078=/ && ($2 in L) && L[$2]++==0)
' searchlist.txt datafile1.txt datafile2.txt

Search the 3078= everywhere:
Code:
awk -F "|" '
# hash the search list
NR==FNR {L[$1]=0; next}
# now procede with the data files
# print if the following is true
(/|3078=/ && ($2 in L) && L[$2]++==0)
' searchlist.txt datafile1.txt datafile2.txt

---------- Post updated at 06:51 AM ---------- Previous update was at 05:35 AM ----------

Quote:
Originally Posted by Lucas_0418
What's your environment, found that your code could work in my cygwin.
Code:
grep -h '4=[745-755]++' *.* | grep '3078=' | sort -u -t'|' -k2,2

May you could put all the pattern in a file, then use option -m of grep to get the first match line.
Code:
while read a
do
grep -h -m 1 "$a" *.*
done< yourpatterfile

grep -m1 exits at every 1st match per file.
awk is much more flexible:
Code:
awk -F "|" -v low=745 -v high=755 '
# build the Lookup hash
BEGIN {for (i=low; i<=high; i++) L["4="i]}
# main loop
# if in Lookup hash and if a field begins with 3078=
($2 in L) && /|3078=/ {
  print
  # delete from the Lookup hash
  delete L[$2]
}
' datafile*.txt


Last edited by MadeInGermany; 03-20-2014 at 08:10 AM..
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting unique values of a column from a feed file

Hi Folks, I have the below feed file named abc1.txt in which you can see there is a title and below is the respective values in the rows and it is completely pipe delimited file ,. ... (4 Replies)
Discussion started by: punpun66
4 Replies

2. UNIX for Dummies Questions & Answers

Grep to find matching patern and return unique values

Request: grep to find given matching patern and return unique values, eliminate the duplicate values I have to retrieve the unique folder on the below file contents like; /app/oracle/build_lib/pkg320.0_20120927 /app/oracle/build_lib/pkg320.0_20121004_prof... (5 Replies)
Discussion started by: Siva SQL
5 Replies

3. Shell Programming and Scripting

compare 2 files and return unique lines in each file (based on condition)

hi my problem is little complicated one. i have 2 files which appear like this file 1 abbsss:aa:22:34:as akl abc 1234 mkilll:as:ss:23:qs asc abc 0987 mlopii:cd:wq:24:as asd abc 7866 file2 lkoaa:as:24:32:sa alk abc 3245 lkmo:as:34:43:qs qsa abc 0987 kloia:ds:45:56:sa acq abc 7805 i... (5 Replies)
Discussion started by: anurupa777
5 Replies

4. Shell Programming and Scripting

How to count Unique Values from a file.

Hi I have the following info in a file - <Cell id="25D"/> <Cell id="26A"/> <Cell id="26B"/> <Cell id="26C"/> <Cell id="27A"/> <Cell id="27B"/> <Cell id="27C"/> <Cell id="28A"/> I would like to know how would you go about counting all... (4 Replies)
Discussion started by: Prega
4 Replies

5. Shell Programming and Scripting

return a list of unique values of a column from csv format file

Hi all, I have a huge csv file with the following format of data, Num SNPs, 549997 Total SNPs,555352 Num Samples, 157 SNP, SampleID, Allele1, Allele2 A001,AB1,A,A A002,AB1,A,A A003,AB1,A,A ... ... ... I would like to write out a list of unique SNP (column 1). Could you... (3 Replies)
Discussion started by: phoeberunner
3 Replies

6. UNIX for Dummies Questions & Answers

Extract Unique Values from file

Hello all, I have a file with following sample data 2009-08-26 05:32:01.65 spid5 Process ID 86:214 owns resources that are blocking processes on Scheduler 0. 2009-08-26 05:32:01.65 spid5 Process ID 86:214 owns resources that are blocking processes on Scheduler 0. 2009-08-26... (5 Replies)
Discussion started by: simonsimon
5 Replies

7. Shell Programming and Scripting

Comparing 2 files and return the unique lines in first file

Hi, I have 2 files file1 ******** 01-05-09|java.xls| 02-05-08|c.txt| 08-01-09|perl.txt| 01-01-09|oracle.txt| ******** file2 ******** 01-02-09|windows.xls| 02-05-08|c.txt| 01-05-09|java.xls| 08-02-09|perl.txt| 01-01-09|oracle.txt| ******** (8 Replies)
Discussion started by: shekhar_v4
8 Replies

8. UNIX Desktop Questions & Answers

Fetching unique values from file

After giving grep -A4 "feature 1," <file name> I have extracted the following text feature 1, subfeat 2, type 1, subtype 5, dump '30352f30312f323030392031313a33303a3337'H -- "05/01/2009 11:30:37" -- -- ... (1 Reply)
Discussion started by: shivi707
1 Replies

9. Shell Programming and Scripting

Unique values from a Terabyte File

Hi, I have been dealing with a files only a few gigs until now and was able to get out by using the sort utility. But now, I have a terabyte file which I want to filter out unique values from. I have a server having 8 processor and 16GB RAM with a 5 TB hdd. Is it worthwhile trying to use... (6 Replies)
Discussion started by: Legend986
6 Replies

10. Shell Programming and Scripting

Getting Unique values in a file

Hi, I have a file like this: Some_String_Here 123 123 123 321 321 321 3432 3221 557 886 321 321 I would like to find only the unique values in the files and get the following output: Some_String_Here 123 321 3432 3221 557 886 I am trying to get this done using awk. Can someone please... (5 Replies)
Discussion started by: Legend986
5 Replies
Login or Register to Ask a Question