egrep -i "(from|to)=<$1>" mail.log ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting egrep -i "(from|to)=<$1>" mail.log ?
# 1  
Old 05-04-2012
egrep -i "(from|to)=<$1>" mail.log ?

Hi,

As you can see I'm trying to get email addresses from the mail.log.

This is a part of a bash shell script I found but I#m wondering how I can check the
variable $1?
When I try
Code:
egrep -i "(from|to)=<$1>" mail.log | awk ' {print $1}'

either from the command line or from a script it doesn't print nothing

Can someone explain me that please?

Thanks

Last edited by Franklin52; 05-04-2012 at 06:36 AM.. Reason: Please use code tags
# 2  
Old 05-04-2012
Please post few lines from mail.log and the desired output. You'll receive quicker responses.
# 3  
Old 05-04-2012
Ok, sorry!

here is an example entry:
Code:
May  3 00:00:29 prx1-mgmt postfix/qmgr[3781]: 88BFC2FB802: from=<double-bounce@prx1.example.com>, size=288, nrcpt=1 (queue active)

So I think $1 should contain "double-bounce@prx1.example.com", right?

Last edited by Franklin52; 05-04-2012 at 06:37 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 05-04-2012
What does not work? What is your OS and version?
Code:
$ echo "$1"
double-bounce@prx1.example.com
$ echo 'May  3 00:00:29 prx1-mgmt postfix/qmgr[3781]: 88BFC2FB802: from=<double-bounce@prx1.example.com>, size=288, nrcpt=1 (queue active)' |
> grep -Ei "(from|to)=<$1>"
May  3 00:00:29 prx1-mgmt postfix/qmgr[3781]: 88BFC2FB802: from=<double-bounce@prx1.example.com>, size=288, nrcpt=1 (queue active)
$

# 5  
Old 05-04-2012
Code:
 
nawk -F"[<>]" '/from/ || /to/ {print $2}' mail.log

# 6  
Old 05-04-2012
The awk cmd is not that important here.

It just picks up a field from the lines matching the regex in the egrep command.

What I'm wondering about is what's the meaning of the $1 variable in the egrep command.
Normally in the regexp (Perl) I use this:
Code:
egrep -i "(from|to)=<(.*)>" mail.log 

if I want to store the contents inside <> and access it then by $1.
Maybe that that confuses me a bit Smilie


Even if (in my opinion) it's not that important in this case, I'm using a RHEL 5.8

Last edited by Franklin52; 05-04-2012 at 09:46 AM.. Reason: Code tags
# 7  
Old 05-04-2012
Ah, now I see what you are trying to do. No grep does not work that way... The $1 is a shell (positional) parameter and it has nothing to do with grep....
It is possible to use back references in egrep, but only to aid in matching, for example:
Code:
$ echo blargdgddd | grep -E '(bla).*\1'
$ echo blagdfgdbla | grep -E '(bla).*\1'
blagdfgdbla


Last edited by Scrutinizer; 05-04-2012 at 10:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

How to find a file which are not ends with ".zip" and which are ends with "*.log*" or "*.out*"?

I am new to bash/shell scripting. I want to find all the files in directory and subdirectories, which are not ends with “.zip” and which are contains in the file name “*.log*” or “*.out*”. I know below command to get the files which ends with “.log”; but I need which are not ends with this... (4 Replies)
Discussion started by: Mallikgm
4 Replies

5. UNIX for Dummies Questions & Answers

Egrep confusion with "I" and "-I" pattern

I am executing following command egrep -w I filename.txt the filename.txt has following data .... -I 07-18 08:31:19.924 9880 6 SessionManager ConnectConfig: ConfigurationWebService LoginResults=SuccessfulLogin I am so hungry that I need to eat I expect egrep to print only the second... (1 Reply)
Discussion started by: VBG
1 Replies

6. Shell Programming and Scripting

Escaping backslash and asterisk in egrep to match "\*"

So far what i've got is egrep '^(\\)\*$'No luck. I've searched the web and not much luck. I know about the escape character \ but its confusing to figure out how to use it to match a backslash and use it to escape the asterisk also. Any ides? Thanks! (8 Replies)
Discussion started by: matthewfs
8 Replies

7. AIX

echo $varibla | mail -s "subject" "xxx@xxx.com" not ruuning as expected

Hi Folks, As per the subject, the following command is not working as expected. echo $variable | mail -s "subject" "xxx@xxx.com" Could anyone figure it out whats wrong with this. I am using AIX box. Regards, (2 Replies)
Discussion started by: gjarms
2 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. UNIX for Dummies Questions & Answers

How to use the "grep/egrep" command to search files.

Hi Team, I am new to this forum and also trying to learn Unix. I will highly appriciate your help if you can help me to get the right command . {{{ I use the command " today | egrep '(10:| 11: )' | grep ERROR " to grep all the files that has been error betweeen 10 to 11... (6 Replies)
Discussion started by: rkhanal
6 Replies

10. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies
Login or Register to Ask a Question