Search and extract matching patterns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search and extract matching patterns
# 8  
Old 02-28-2012
Here, try this, the match part added (red) is kinda foggy but test it.

Code:
NR==FNR {
split($3,a,","); for (i in a) vl[a[i]] = $1 FS $2 FS vl[a[i]]
}
NR!=FNR {
idx=$1
OFS="\t"
if ( idx in vl ) {
        final[vl[idx]] = final[vl[idx]] OFS idx
        }
}
END {
        for ( z in final ) {
                if (match(final[z],/\t*_ent\t/)) 
                print z OFS final[z]
                }
}

# 9  
Old 02-28-2012
Code:
perl -lane 'open F1, "< file1.tab";
for $f1 (<F1>) {
    chomp $f1;
    if ($F[2] =~ /$f1/) {
        open F2, "< file2.tab";
        for $f2 (<F2>) {
            chomp $f2;
            if ($F[2] =~ /$f2/) { print "$F[0]\t$F[1]\t$f1\t$f2" }
        }
        close F2;
    }
}
close F1' bigdb.tab

# 10  
Old 02-28-2012
Thanks Peasant !

Something weird happens.

It works fine on the example files I gave you but not on my real files. It returns nothing !

I checked all my files several times. They have exactly the same format and everything, except the file names.
I really don't understand

---------- Post updated at 04:01 AM ---------- Previous update was at 03:10 AM ----------

Thank balajesuri !

It works on the example files.

But I am trying them on my real files as well (several files called file1a.tab, file1b.tab, file1c.tab, etc...).

In order to run the script on several files at once , is it possible to modify your script (in red) like that in a file called test.sh:
Code:
for U in file1*.tab
do
     V=`basename ${U} .tab`
perl -lane 'open F1, "< ${U}";
for $f1 (<F1>) {
    chomp $f1;
    if ($F[2] =~ /$f1/) {
        open F2, "< file2.tab";
        for $f2 (<F2>) {
            chomp $f2;
            if ($F[2] =~ /$f2/) { print "$F[0]\t$F[1]\t$f1\t$f2" }
        }
        close F2;
    }
}
close F1' CPDB.tab
> ${V}_results.tab
done

and run the file:
Code:
sh test.sh

to obtain ouput files for each files1*.tab ?

---------- Post updated at 04:04 AM ---------- Previous update was at 04:01 AM ----------

Actually, I just checked.
All the files file1*_results.tab are empty...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extended grep not matching some patterns

i have a file where the hostnames and variables are in same line in below format, am able extract some part variables while otherlike subscriptions and handler is missing. can you please correct me if grep is able to perform this ? cat /tmp/test localhost subscriptions='' handler="genie"... (14 Replies)
Discussion started by: rakeshkumar
14 Replies

2. Shell Programming and Scripting

Delete patterns matching

Delete patterns matching OS version: RHEL 7.3 Shell : Bash I have a file like below (pattern.txt). I need to delete all lines starting with the following words (words separated by comma below) and ) character. LOGGING, NOCOMPRESS, TABLESPACE , PCTFREE, INITRANS, MAXTRANS, STORAGE,... (3 Replies)
Discussion started by: John K
3 Replies

3. Shell Programming and Scripting

How can I extract XML block around matching search string?

I want to extract XML block surrounding search string Ex: print XML block for string "myapp1-ear" surrounded by "<application> .. </application>" Input XML: <?xml version="1.0" encoding="UTF-8"?> <deployment-request> <requestor> <first-name>kchinnam</first-name> ... (16 Replies)
Discussion started by: kchinnam
16 Replies

4. Shell Programming and Scripting

Finding matching patterns in two files

Hi, I have requirement to find the matching patterns of two files in Unix. One file is the log file and the other is the error list file. If any pattern in the log file matches the list of errors in the error list file, then I would need to find the counts of the match. For example, ... (5 Replies)
Discussion started by: Bobby_2000
5 Replies

5. Shell Programming and Scripting

awk extract strings matching multiple patterns

Hi, I wasn't quite sure how to title this one! Here goes: I have some already partially parsed log files, which I now need to extract info from. Because of the way they are originally and the fact they have been partially processed already, I can't make any assumptions on the number of... (8 Replies)
Discussion started by: chrissycc
8 Replies

6. UNIX for Dummies Questions & Answers

Find records with matching patterns

Hi, I need to find records with a search string from a file. Search strings are provided in a file. For eg. search_String.txt file is like below chicago mexico newark sanhose and the file from where the records need to be fetched is given below src_file:... (1 Reply)
Discussion started by: sbhuvana20
1 Replies

7. Shell Programming and Scripting

print lines between 2 matching patterns

Hi Guys, I have file like below, I want to print all lines between test1231233 to its 10 occurrence(till line 41) test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq23 test1231233 qwe qwe qweq123 test1231233 qwe qwe qweq123131 (3 Replies)
Discussion started by: jagnikam
3 Replies

8. Shell Programming and Scripting

Matching patterns

I have a file name in $f. If $f has "-" at the beginning, or "=", or does not have extension ".ry" or ".xt" or ".dat" then cerr would not be empty. Tried the following but having some problems. set cerr = `echo $f | awk '/^-|=|!.ry|!.xt|!.dat/'` (4 Replies)
Discussion started by: kristinu
4 Replies

9. Shell Programming and Scripting

AWK: matching patterns in 2 different files

In a directory, there are two different file extensions (*.txt and *.xyz) having similar names of numerical strings (*). The (*.txt) contains 5000 multiple files and the (*.xyz) also contains 5000 multiple files. Each of the files has around 4000 rows and 8 columns, with several unique string... (5 Replies)
Discussion started by: asanjuan
5 Replies

10. Shell Programming and Scripting

removing certain paragraphs for matching patterns

Hi, I have a log file which might have certain paragraphs. Switch not possible Error code 1234 Process number 678 Log not available Error code 567 Process number 874 ..... ...... ...... Now I create an exception file like this. cat text.exp Error code 1234 Process number 874 (7 Replies)
Discussion started by: kaushys
7 Replies
Login or Register to Ask a Question