LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2012, 09:23 AM   #1
dznunn
LQ Newbie
 
Registered: Jul 2010
Posts: 1

Rep: Reputation: 0
Searching for multiple patterns with grep


Hi

I'm having trouble with grep and am looking for some assistance.

I'm trying to retrieve certain information from whois output. I need to grep for 3 strings, but return only the first match for 2 of them. How can I achieve this?

I'm currently running:

whois tucows.com|egrep -e 'Domain Name:|Name Server:|Registrar:'

Which returns:

Registrar: MONIKER ONLINE SERVICES, INC.
Domain Name: TUCOWS.COM
Registrar: TUCOWS.COM CO.
Name Server: NS1.TUCOWS.COM
Name Server: NS2.TUCOWS.COM
Name Server: NS3.TUCOWS.COM


I want to return the first matches for Registrar: and Domain Name:

Thanks for any help in advance.
Dave
 
Old 06-26-2012, 09:51 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
sed may be a better tool to use.

whois | grep '/Domain Name:/,/^$/ { /Domain Name/p; /Registrar:/p;/Name Server:/p }'

When I run whois' I don't get a separate registrar before the whois block of information.

You might want to add the command '/<<</q' to quit before the voluminous information is reached after the whois block of results.
 
Old 06-26-2012, 09:52 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Use awk:
Code:
whois tucows.com | awk -F ':' '{ key = tolower($1); gsub(/[^0-9a-z]+/, "", key) } (key=="domainname" && dn==0) { dn=1; print $0 } (key=="registrar" && r==0) { r=1; print $0 } (key == "nameserver")'
The same expanded a bit, to make it easier to read and edit (but still works if pasted on command line, I even kept the necessary semicolons so you can pack it back to a single long line):
Code:
whois tucows.com | awk -F ':' '#
  {  key = tolower($1) ;
     gsub(/[^0-9a-z]+/, "", key)
  }
  (key=="domainname" && dn==0) {
     dn = 1 ;
     print $0
  }
  (key=="registrar" && r==0) {
     r = 1 ;
     print $0
  }
  (key=="nameserver")
  '
The -F option sets the field separator, here to a colon.

The hash mark starts a comment. Some awk variants don't like the first line to be empty, so it is common to put the hash mark there.

Each rule is applied to each record (line).

The first rule takes the first field (everything before the first colon), converts it to lower case, and removes all non-alphanumeric characters. It will be accessible to other rules too, as key. It makes the comparisons more robust later on, but it might be overkill.

The second rule matches when key is "domainname" for the first time. The rule just causes the record to be output.

The third rule matches when key is "registrar" for the first time.

The fourth rule matches when key is "nameserver". There is no need to explicitly write the body of the rule { print $0 } since that is the default action. (If the rule clause evaluates to true, and there is no body, then the record is output. Otherwise the rule is skipped.)
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep many files in multiple directories using patterns from a file francy_casa Linux - Newbie 4 04-12-2012 08:49 AM
[SOLVED] Search multiple patterns & print matching patterns instead of whole line Trd300 Linux - Newbie 29 03-05-2012 07:41 PM
Excluding multiple patterns from grep flamingo_l Linux - General 9 01-06-2011 08:05 AM
grep for multiple patterns???? lucastic Linux - Software 4 08-06-2010 06:07 PM
searching multiple patterns through Vi editor barunparichha Linux - Software 10 05-12-2009 08:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration