0

I am trying to extract values from email header, as the Message ID

The grep do not properly use the previous variable...

Here is my code:

for email in $1/*.eml
do

MSGID=$(cat ${email} | grep -i 'Message-ID:' |awk '{print $2}')
M=$(echo "\""${MSGID}"\"")
SRV="/var/log/maillog"
echo "${M}"

VER=$(grep -i ${M} ${SRV} | tail -n 1)
echo ${VER}
done

I am trying to re-encapsule the initial variable MSGID with M variable; Unfortunately I still could not figure this out.

Initial value (input) looks like this:

Message-ID: <COMPUTER123DDF38c28d5e776e4fbdb82e2e92bd9a4373@computer123ddf>

The MSGID keep only the string as:

<COMPUTER123DDF38c28d5e776e4fbdb82e2e92bd9a4373@computer123ddf>

Then I would like to use this string against a file using grep. Unfortunately the VER command does not trigger anything.

I thought that adding "\""${MSGID}"\"" would/should have keep the initial string untouched.

3
  • There are a lot of redundant steps in this script. Please edit your question to provide a sample of your input and the desired output. Commented Mar 2, 2015 at 11:53
  • Please show some input file together with your expected output. Maybe it is worth not making your current script the starting point, since for example awk may handle it in a way straight-forward way. Commented Mar 2, 2015 at 11:54
  • Probably MSGID=$awk '/^Message-ID:/ {print $2}' file) would be faster. Also, note SRV can be declared outside the for loop, since it is a fixed value. Commented Mar 2, 2015 at 12:18

3 Answers 3

2

It's not really clear what you are attempting to accomplish, but just removing the weird stuff simplifies to a script which might help you finish your task.

SRV="/var/log/maillog"
for email in $1/*.eml; do
    M=$(awk 'tolower($0) ~ /message-id:/ {print $2}' "$email")
    echo "$M"
    grep -i "$M" "$SRV" | tail -n 1
done

This has a number of problematic corner cases. The Message-Id extraction should probably be stricter:

M=$(awk 'tolower($1) ~ /^message-id:/ { print $2 } /^$/ { exit(0) }' "$email")

and if the extracted Message-Id contains regex metacharacters, you should escape them, or perhaps use proper grep flags:

grep -iF "$M" "$SRV" | tail -n 1

To be sure, the double quotes in grep "$string" file are not part of the syntax for grep; they are discarded by the shell before grep runs, and are useful for preventing the shell from messing with whitespace and/or shell metacharacters in the value of $string. When you forced in another pair of double quotes, you were grepping for the Message-id inside literal double quotes in the log file, and (of course) not finding any.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know the lc() function in awk. To lowercase you probably want to use tolower(). Note you can also say IGNORECASE=1 in the BEGIN{} block.
0

Try this solution

 M="\""$MSGID"\""

Comments

0

Thank you all. And sorry.

I have figured that there were a carriage return in my initial value.

I have changed this like this:

MSGID=$(cat ${email} | grep -i 'Message-ID:' |awk '{print $2}' tr -d '\r')

Thank you again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.