3

I'm a complete noob at awk/sed so forgive me if I'm missing something obvious here.

Basically I'm trying to do a nested grep, i.e. something akin to:

grep $value `exim -Mvh $(`exim -bpru | grep $eximID | more`)`

Breakdown:

grep $value IN COMMAND 
--> exim -Mvh (print exim mail headers) FROM RESULTS OF 
    ---> exim -bpru | grep $eximID | more
  • $value is the string I'm looking for
  • $eximID is the string I'm looking for within exim -bpru (list all exim thingies)

No idea if what I'm trying to accomplish would be easier with awk/sed hence the question really.

I tried to make that as legible as possible but nested nesting is hard yo

Edit Tada! My script is now workings thanks to you guys! Here it is, unfinished, but working:

#!/usr/bin/bash

    echo "Enter the email address you want to search for + compare sender info via exim IDs."
    read searchTarget
    echo "Enter the target domain the email is coming from."
    read searchDomain

    #domanList is array for list of exim IDs needed
    domainList=($(exim -bpru | grep "$searchDomain" | awk '{ print $3 }'))


    for i in "${domainList[@]}"
            do
                    echo "$(exim -Mvh  $i | grep $searchTarget)"
                    #echo "$(grep $searchTarget $(exim -Mvh $i))"
            done
nom
  • 55
  • 1
  • 4
  • It is hard to interpret what you are asking -- some examples would be nice. For instance, show us some example data and what the desired output is. – csiu Feb 22 '14 at 03:38
  • It maybe possible to do what you want with a pipe. As is: `grep foo * | grep -v "something_to_ignore" | grep -i potatoe` – Red Cricket Feb 22 '14 at 03:42

2 Answers2

4
grep $value `exim -Mvh $(`exim -bpru | grep $eximID | more`)`

This isn't right. The backticks (`command`) and $(command) do the same thing, it's just an alternative syntax. The advantage of using $() is that it's better nestable, so it's a good habit to always use that.

So, let's fix this, we now end up with:

grep "$value" "$(exim -Mvh "$(exim -bpru | grep "$eximID")")" | more

I relocated the more command, for what I think will be obvious reasons. more just paginates data for the user, feeding the output of more to something else almost never makes sense.

I've also quoted the variables, this is also a good habit, because otherwise things will break when there are certain characters in your variable (most common is the a space).

I can't test if this gives you the output you want, if it doesn't, then update your answer with a few lines of example data, and the expected output.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 1
    i think it needs a closing round bracket ) as they are not matching – HackerKarma Feb 22 '14 at 03:47
  • Great answer! One last problem.... why is the script I'm trying to right giving me: "grep: Only: No such file or directory grep: one: No such file or directory grep: message: No such file or directory grep: can: No such file or directory grep: be: No such file or directory grep: listed: No such file or directory grep: at: No such file or directory grep: once: No such file or directory" http://pastebin.com/82bqzEw5 edit - I had that 'eurika' feeling for all of about 30 seconds until this )'; – nom Feb 22 '14 at 04:14
  • @nom Your command effectively looks like: `grep "$value" filename`, where `filename` is the entire `$()` command substitution, which is not what you want, judging from your script. I'm not quite sure what your `exim` commands output, but you may want to add the grep to the end like so: `exim -Mvh $(exim -bpru | grep "$searchDomain") | grep "$searchTarget"` ... – Martin Tournoij Feb 22 '14 at 04:26
  • Scratch that, it's a problem with how exim wants to display the headers of a subject via ID. Now to create a for loop to display them 'individually' -.- edit -- interestingly enough, adding grep at the end doesn't give anything back, not even the error: exim -Mvh $(exim -bpru | grep "$searchDomain" | awk '{ print $3 }') | grep "$searchTarget" – nom Feb 22 '14 at 04:26
  • There are no quotes. That's how it should be: ``"$( "$(... "$var")" )"`` – Aleks-Daniel Jakimenko-A. Feb 22 '14 at 11:29
  • @Aleks-DanielJakimenko You're right. I forgot that. Thank you for correcting. – Martin Tournoij Feb 22 '14 at 17:32
0

If you're going to do it with back-quotes (not recommended; it is hard work), then you have to write:

grep $value `exim -Mvh $(\`exim -bpru | grep $eximID\`)`

(where I've removed the more since when used like that it behaves like cat and there's no point in using cat at the end of the commands like that either).

It would be more sane to use the $(…) notation throughout:

grep $value $(exim -Mvh $( $(exim -bpru | grep $eximID)))

And it seems more plausible that you don't need quite that many sets of indirection and this is what you're really after:

grep $value $(exim -Mvh $(exim -bpru | grep $eximID))

You should look at:

and no doubt there are other related questions too.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278