selective grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers selective grep
# 1  
Old 01-23-2012
selective grep

Hello,

I have been using the command below for file manipulation.

Code:
while read A B; do grep $f1; done < f2 > f3

So, if a certain string is found in f2 (for ex; DOG243435) and it is also present in f1, then print that string plus the contents of the line in which it was found onto f3.

this works great, however, I wanted to know how I can avoid grepping for a string if a certain phrase is stated before that string. In the example below, if the word contained appears, I do not want to grep the string "DOG23244" after it.. How can I avoid only grepping DOG00912 and the contents of that line and NOT DOG23244 if the word contained appears?

Quote:
12 aasa 1d "DOG23434"
13 aass 2d "DOG00912" contained in "DOG23244"
# 2  
Old 01-23-2012
I can't imagine how that command would work. grep would be eating all the lines out of your f2 file, causing the loop to only run once, and your A and B variables don't get used anywhere, at all, ever.

Show an example of the input you have and the output you want, that will be more helpful than strangely broken code.
# 3  
Old 01-24-2012
Sorry, the command is:

Code:
while read A B; do grep $A f1; done < f2 > f3

---------- Post updated 01-24-12 at 08:27 AM ---------- Previous update was 01-23-12 at 04:32 PM ----------

The input I have is this:


f1 looks like this

Quote:
12 aasa 1d "DOG11111"
13 aass 2d "DOG00912" contained in "DOG11111"
14 sdsss 3s "DOG00231" contained in "DOG00912"
34 dd 4d "DOG02340
f2 looks like this:

Quote:
DOG11111
DOG00231
DOG00912
Now, f3 should look like this:

Quote:
12 aasa 1d "DOG11111"
14 sdsss 3s "DOG00231" contained in "DOG11111"
13 aass 2d "DOG00912" contained in "DOG23244"
BUT, f3 is looking like this:

Quote:
12 aasa 1d "DOG11111"
14 sdsss 3s "DOG00231" contained in "DOG11111"
12 aasa 1d "DOG11111"
13 aass 2d "DOG00912" contained in "DOG23244"
Notice how the line '12 aasa 1d "DOG11111"' appears twice....this is the issue I am having. Because there are two DOG strings in the same line in some occasions, I do not want to grep for the DOG string that appears after the word "contained", meaning that if the code matches up a string between f1 and f2 it should be printed in f3 BUT if a line has a DOG string and the phrase "contained" only the first DOG of that line should be printed.


I basically just want grep to ignore the DOG string that appears after the word "contained"
# 4  
Old 01-24-2012
Could I suggest using grep with the -f flag?
Code:
grep -f f2 f1 > f3


Does that acheive what you want? It should find any lines in f1 that match a record in f2 and write the output to f3 - or have I missed the point?




Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 01-24-2012 at 10:02 AM.. Reason: Spelling
# 5  
Old 01-24-2012
Hi rbatte1, your code doesn't seem to be working for my situation. I have been using an analogous code that has been working for me

Code:
while read A B; do grep $A f1; done < f2 > f3

However, I do not want to grep for the DOG string after the word contained...How can I get around that?
# 6  
Old 01-24-2012
Well, you'll have to split it into the tokens you want first, or it'll take the whole line( half of which you're not using in your example ) as the pattern. Since you appear to be splitting on spaces:
Code:
awk '{ print $1 }' filename > /tmp/$$

grep -f /tmp/$$ filename | grep -v DOG

rm -f /tmp/$$

# 7  
Old 01-24-2012
Well, you didn't say that bit. It's still a little unclear though. Do you mean you don't want:-
  • any lines containing the word contained
  • any text from the word contained to the end of the line
For these two, you could try:-
Code:
grep -f f2 f1 | grep -v contained > f3

or
Code:
grep -f f2 f1 | cut -d " " -f-4 > f3

Do either of these address your needs? If not, please be explicit and show the exact output you are after.


I hope that this helps,
RObin
Liverpool/Blackburn
UK
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selective running

I have MegaCli64 -PDList -aALL | grep -E 'Enclosure Device ID|Slot Number' Enclosure Device ID: 252 Slot Number: 0 Enclosure Device ID: 252 Slot Number: 1 Enclosure Device ID: 252 Slot Number: 2 Enclosure Device ID: 252 Slot Number: 3 I need to run the following command on all slots... (4 Replies)
Discussion started by: anil510
4 Replies

2. Shell Programming and Scripting

Selective printing

I have the following contents in a file ---- CRITICAL: altered for /usr/bin/bin1 ---- OK: /usr/sbin/bin2 result fine ---- OK: /usr/sbin/bin3 result fine ---- CRITICAL: altered for /usr/bin/bin4 ---- OK: /usr/bin/bin5 result fine ---- OK: /usr/bin/bin6 result fine ---- CRITICAL: altered for... (9 Replies)
Discussion started by: anil510
9 Replies

3. UNIX for Dummies Questions & Answers

Selective replace

i have a large sequence of format sat_1_g3_g_0_8540 . A 1 15501 . . . ID=sat_1_g3_g_0_8540;parentName=sat_1_g3_g_0_8540;Al=sat_1_g2_g_0_8540; sat_1_g3_g_2_8510 . C 1 25501 . . . ... (11 Replies)
Discussion started by: siya@
11 Replies

4. Shell Programming and Scripting

Selective grouping

I have a text file in this format. Group: AAA Notes: IP : 11.11.11.11 #User xxxxxxxxx #Password aaaaaaaaaaaaaaaa Group: AAA Notes: IP : 11.11.11.22 #User yyyyyyyyyyyyy #Password bbbbbbbbbbbbb (8 Replies)
Discussion started by: anil510
8 Replies

5. Shell Programming and Scripting

Selective grep

I have to grep out only email address from a column. It has characters appended and prepended F=<sss1@domain.com> <sss2@domain.com> (sss3@domain.com) <sss4@domain.com> Whatever added before and after email, I should be able to grep out only emails. (9 Replies)
Discussion started by: anil510
9 Replies

6. UNIX for Dummies Questions & Answers

Help with selective ls

Hi all :wall: Can anyone advise how do I use ls to do a selective amd sorted listing of file that I want to have as below? Am looking for files that are named as log_<nnnn>.txt, where <nnnn> are numeric, i.e. I want to have a listing sorted from the newest to the oldest of files that... (7 Replies)
Discussion started by: newbie_01
7 Replies

7. Shell Programming and Scripting

selective printing

hi all from below text "abcd,SYS_12345,xyz,PQR, ," I want to print only "abcd,SYS,xyz,PQR, ," i.e. taking only first three 3 chars from 2 string of comma separated file thanks (4 Replies)
Discussion started by: JoeColeEPL9
4 Replies

8. IP Networking

selective masquerading

I am running Linux 2.6.20 on i686 architecture. I want to be able to masquerade different hosts on my LAN to different external interfaces. Specifically, I want one specific host to masquerade through a vpn tunnel while the other hosts simply masquerade over the regular ISP interface. I only... (4 Replies)
Discussion started by: NESter
4 Replies

9. UNIX for Advanced & Expert Users

Selective Umask

Hi, We have umask 022 in /etc/profile for security reasons. But I want have some other umask for selected users. how to achieve this? Regards (3 Replies)
Discussion started by: baanprog
3 Replies

10. Shell Programming and Scripting

echo is selective?

i tried to ask about this earlier, but didn't explain well enough. From a script, i issue the following: echo `pgp --encrypt filename --recipient public-key` The result is perfect. The encrypted file is in the directory just as if i'd issued the pgp command from the command line. However,... (0 Replies)
Discussion started by: rgard
0 Replies
Login or Register to Ask a Question