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.)