Tricky GREP question..


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Tricky GREP question..
# 1  
Old 02-17-2012
Question Tricky GREP question..

I have some large login files that I need to extract (user)@(server) from. Where it gets tricky is that there is usually more than one '@' sign on each line(although it does have a leading space if it's not part of the (user)@(server) string), I need only the (user)@(server) section, I need only unique occurrences, AND I need to drop the ones where the (server) is an IP address. Here are a few sample lines from the file:

Code:
2012-02-17 00:07:49 ABC[generic04]: [KJEHKJNSEDS0] jdhws@bosun01: Request connection
2012-02-17 00:07:49 ABC[generic04]: [KJEHKJNSEDS0] jdhws@bosun01: connection closed
2012-02-17 00:07:49 ABC[generic02]: [KJJEHWSEDS0] hicks@bosun02: Request connection
2012-02-17 00:07:49 ABC[generic04]: [KJEHKJNSEDS0] star@192.168.1.254: Reset busy
2012-02-17 00:07:49 ABC[generic04]: jdhws@bosun01: [KJEHKJNSEDS0]  Connection running

The output I would need the command to generate from these lines would be:
Code:
jdhws@bosun01
hicks@bosun02

Can it be done?

Last edited by methyl; 02-17-2012 at 06:36 PM.. Reason: please use code tags
# 2  
Old 02-17-2012
Does it have to be grep?
Code:
perl -nle 'print $& if (/\w+@\w+/&&!/@\d/)' file | sort | uniq

# 3  
Old 02-17-2012
Seems like sed and sort could do it, but are your local parts simple or quoted and with comments: Email address - Wikipedia, the free encyclopedia

That takes more of a parser to keep track of state vis a vis quotes and escapes.

For just letters, numbers, dot and _:

Code:
sed '
    s/[a-zA-Z0-9_.]\{1,99\}@[-a-zA-Z0-9.]*[a-zA-Z][-a-zA-Z0-9.]*/\
&\
/g
    t
    d
  ' your_file | fgrep '@' | sort -u

Narrative: Have sed put line feeds before and after any such email address found with the host containing with a letter in it, grep out any lines with at-sign and sort unique.

Last edited by DGPickett; 02-17-2012 at 06:00 PM..
# 4  
Old 02-19-2012
Do you have grep -o ?
Code:
grep -o '[^[:space:]]*@[^:]*[[:alpha:]][^:]*' file | sort -u

# 5  
Old 02-22-2012
Quote:
Originally Posted by Scrutinizer
Do you have grep -o ?
Code:
grep -o '[^[:space:]]*@[^:]*[[:alpha:]][^:]*' file | sort -u

Alas, no. Smilie I am running AIX 5.3, and evidently, the '-o' option is not there. It throws an error.

---------- Post updated at 11:32 AM ---------- Previous update was at 11:17 AM ----------

Quote:
Originally Posted by bartus11
Does it have to be grep?
Code:
perl -nle 'print $& if (/\w+@\w+/&&!/@\d/)' file | sort | uniq

This is perfect. Is there a way to alter this statement, so that it will also pull the addresses with IP addresses instead of server names? (my requirement has changes.. *sigh*)
# 6  
Old 02-22-2012
Try:
Code:
perl -nle 'print $& if (/\w+@[\w.]+/)' file | sort | uniq

# 7  
Old 02-22-2012
Disregard. It helps if I actually put the code in properly.

---------- Post updated at 01:42 PM ---------- Previous update was at 01:33 PM ----------

Everyone who responded: I want to thank you all for helping me with this. It is refreshing to come to a forum, ask a question, and not be met with "rtfm" or similar. In the interests of my own education, and betterment, can someone explain exactly what is going on with this statement in perl?:

Quote:
Originally Posted by bartus11
Try:
Code:
perl -nle 'print $& if (/\w+@[\w.]+/)' file | sort | uniq

This one worked beautifully. Again, thank you all.

Last edited by Mordaris; 02-22-2012 at 03:40 PM.. Reason: I'm stupid.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tricky BASH quoting question

I have some data files that I can identify by a certain pattern in the names using find. Every one of those data files has an XML file associated with it (can be multiple data files per XML file). The XML file is always up one directory from the data file(s) in a folder calledRun##### -... (12 Replies)
Discussion started by: Michael Stora
12 Replies

2. Shell Programming and Scripting

Tricky recursive removal (find with grep)

Tricky one: I want to do several things all at once to blow away a directory (rm -rf <dir>) 1) I want to find all files recursively that have a specific file extension (.ver) for example. 2) Then in that file, I want to grep for an expression ( "sp2" ) for example. 3) Then I want to... (1 Reply)
Discussion started by: jvsrvcs
1 Replies

3. Shell Programming and Scripting

grep variable with tricky content

Hello, i have this issue: text="-8x7YTVNk2KiuY-PWG5zzzjB-zzw" string=-8x7YTVNk2KiuY-PWG5zzzjB-zzw echo $text | grep -v \'$string\' -8x7YTVNk2KiuY-PWG5zzzjB-zzw echo \'$string\' '-8x7YTVNk2KiuY-PWG5zzzjB-zzw' ..and ofcourse if I do like this : echo $text | grep -v $string grep: invalid... (5 Replies)
Discussion started by: black_fender
5 Replies

4. Solaris

Tricky egrep

Hi folks! My first post here. I'm working on a script that retrieves a range of files from a list depending on a range of time. UPDATE: I've seen it could be difficult to read all this thing, so I'll make a summarize it.. How come I do this and take a result.. grep "..\:.." lista.new |... (4 Replies)
Discussion started by: kl0x
4 Replies

5. Shell Programming and Scripting

Another tricky sed or awk question

This post is in reference to https://www.unix.com/shell-programming-scripting/137977-tricky-sed-awk-question-post302428154.html#post302428154 I am trying to go the opposite direction now: I have the following file: a,b,C,f,g a,b,D,f,g a,b,E,f,g h,i,J,k,l m,n,O,t,u m,n,P,t,u m,n,Q,t,u... (3 Replies)
Discussion started by: awayand
3 Replies

6. Shell Programming and Scripting

Tricky sed or awk question

Hello everyone, unfortunately I am no unix nor scripting guru, which is why I am asking for help here. I am trying to reformat a .csv file using sed or awk which has the following format: a,b,C-D-E,f,g h,i,J,k,l m,n,O-P-Q-R-S,t,u v,w,X-Y,z,a It's basically a 5-field text file which has an... (7 Replies)
Discussion started by: awayand
7 Replies

7. UNIX for Dummies Questions & Answers

Tricky Quotation Question

Hi, I am at a point in my script where I defined the number of the command line parameter I would like to set a variable equal to: parameter_number=14 I would then like to set a variable equal to the correct parameter: variable=$parameter_number The issue here is that {} is required... (2 Replies)
Discussion started by: msb65
2 Replies

8. Shell Programming and Scripting

Tricky script question

Hi, I'm in the midst of writing a UNIX script that sftp's files to an external host and am stuck with a problem. The problem is that the files created on my server as a order number that correlates to a sequence of directories on the remote host which is where the file should be ftp'ed. ... (3 Replies)
Discussion started by: budrito
3 Replies

9. Windows & DOS: Issues & Discussions

Tricky one...

Here's my problem: I have a laptop running Windows XP Pro with no internal CD or Floppy drives. I want to install Linux on it. I don't care about the Windows XP Pro installation, in fact I would like to install Linux over the entirety of the HD. However I cannot boot from any external CD drive... (1 Reply)
Discussion started by: saabir
1 Replies

10. Filesystems, Disks and Memory

Tricky File Permission Question

I'm trying to answer the following question about file permissions in Unix. Consider a file with the following permissions: rwx---r-- I am not the owner of this file, but I am a member of the group of this file. My question is: do I have read access to this file? I thought... (3 Replies)
Discussion started by: Hook
3 Replies
Login or Register to Ask a Question