0

Q: How do I pipe multi-results of first search to search a second file.

Ex:

1) FILE1
system abc{
system ghi{
....
...

2) FILE2
define ABC_1_SUFFIX  0XF67
define ABC_2_SUFFIX  0XF34
define DEF_1_SUFFIX  0XF65
define DEF_2_SUFFIX  0XF11
define GHI_1_SUFFIX  0XF73
define GHI_2_SUFFIX  0XF82  


grep -r "^system" file | awk '{print $2}'   
grep -r "#define.*<result of 1st grep>" file2

Task: How do I pass result of first grep (which is abc,def,ghi against grep -r "^system" ) inside second grep. I know pipe/xargs but cant figure this out yet.

I did search related topic but solution didnt work out.
1] GREP by result of awk
2] grep using grep results

Thanks in advance

  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Jul 07 '20 at 20:39
  • Are you expecting multiline output from the first grep and want to match each? It feels like you want `xargs`, but it's not really clear what you're trying to do. – William Pursell Jul 07 '20 at 20:44
  • @WilliamPursell, yes .. so the second grep is like grep'ing over loop of results of first. I have not used xargs in a grep on a file, could you please let me know how I can use same. Thanks for your hints – sreekesh padmanabhan Jul 07 '20 at 23:34
  • Sorry, it's still not at all clear what you want. From the sample files you've shown, it seems like the solution you are looking for is `cat FILE2`. – William Pursell Jul 08 '20 at 15:54
  • @WilliamPursell I removed "system def{" from FILE1. Now, the result should be all address related to abc and ghi only, omitting def. A script will work it but trying for a one liner hack. Pls let me know. – sreekesh padmanabhan Jul 08 '20 at 20:09

2 Answers2

1

It's still not really clear exactly what you want, but perhaps something like:

$ cat file1
system abc{
system ghi{
$ cat file2
define ABC_1_SUFFIX  0XF67
define ABC_2_SUFFIX  0XF34
define DEF_1_SUFFIX  0XF65
define DEF_2_SUFFIX  0XF11
define GHI_1_SUFFIX  0XF73
define GHI_2_SUFFIX  0XF82
$ awk '/^system/ && NR==FNR { gsub("[^a-zA-Z]","",$2); 
    a[toupper($2)] = 1; next } 
    /^define/ {for(k in a) if(match($0,k)) print $3}' file1 file2
0XF67
0XF34
0XF73
0XF82
William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

Do you mean passing the first command's output to the second command itself?

Maybe something like this?

fisrt="$(grep -r "^system" file)"
second="$(grep -r "#define.*${first}" file2)"

Ugly one-liner:

grep -r "#define.*$(grep -r "^system" file)" file2
ΔO 'delta zero'
  • 3,506
  • 1
  • 19
  • 31