0

I have this file

-   .   ID  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   2   Parent  =   tom_fa_10005086
-   1   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
+   .   ID  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
-   .   ID  =   tom_fa_10000024
-   0   Parent  =   tom_fa_10000024
-   .   ID  =   tom_fa_10004587
-   0   Parent  =   tom_fa_10004587
-   1   Parent  =   tom_fa_10004587

and these patterns

COL 1           

tom_fa_10005086
tom_fa_10013928
tom_fa_10000024
tom_fa_10011338
tom_fa_10003474

I want extract the lines when the pattern match with lines and keep it in a file.

But I have 150 patterns and I need that every match is saved it in a different files? so I have I have 150 patterns, I would have 150 outputs files.

The results would look like:

File 1

   -   .   ID  =   tom_fa_10005086
    -   0   Parent  =   tom_fa_10005086
    -   0   Parent  =   tom_fa_10005086
    -   2   Parent  =   tom_fa_10005086
    -   1   Parent  =   tom_fa_10005086
    -   0   Parent  =   tom_fa_10005086
    -   0   Parent  =   tom_fa_10005086

File 2

+   .   ID  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928

file 3 ......

I was thinking about:

grep -f file 1 file 1 | ....

grep -E

0

Given a patterns file:

$ cat patterns
tom_fa_10005086
tom_fa_10013928
tom_fa_10000024
tom_fa_10011338
tom_fa_10003474

Then

awk 'NR==FNR{a[$1]=NR; next} $NF in a {print > "outfile" a[$NF]}' patterns file

results in the following files:

$ head outfile?
==> outfile1 <==
-   .   ID  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   2   Parent  =   tom_fa_10005086
-   1   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086
-   0   Parent  =   tom_fa_10005086

==> outfile2 <==
+   .   ID  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   0   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   1   Parent  =   tom_fa_10013928
+   2   Parent  =   tom_fa_10013928

==> outfile3 <==
-   .   ID  =   tom_fa_10000024
-   0   Parent  =   tom_fa_10000024
0

I tried with below methods and it worked fine too

for i in `cat patternfile`; do sed -n '/'$i'/p' examplefile >file_$i.txt ; done


examplefile===> datafile
patternfile===> Consists of data which contains pattern which need to be searched in examplefile

after each pattern search output will be saved in "file_pattern.txt"
0

Assuming that your first, large file is named target.txt and your second smaller file is named source.txt.

tail +3 source.txt | while IFS= read -r line; do grep "$line" target.txt > "$line"; done

Explanation

  • tail +3 source.txt: remove the first two lines of source.txt
  • | while IFS= read -r line; do <FOO>; done: pipe this truncated file into the while read construct. This will iterate over each line of the truncated source.txt, doing <FOO> for each line.
  • grep "$line" target.txt > "$line": for each source.txt line, grep for it in target.txt, then write to a file named the same as the original string that you searched for.

A minor comment: if source.txt actually contains fixed strings instead of patterns, you can use the -F option for grep.

  • thanks, this ones was so helpful, It works perfectly, but I removed "results/" – Juliana B C Apr 26 at 15:34
  • @JulianaBC I edited to remove the subdirectory part. I notice that you are new here (welcome!). FWIW on StackExchange sites, you can say "thank you" by upvoting answers (clicking the up arrow on the left of answers). Cheers! – Sparhawk Apr 26 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.