help! Pattern Matching in vi or sed

I have a bunch of conf files, that contain the fully qualified names of servers. I would like to be able to use some sort of pattern matching with sed or vi, or whatever, to pull out the fully qualified server names, and dump them in a file.

It just needs to work across several unix os. So, I have been using vi. That works, but it takes about 8 pattern matches to whittle the conf file down to just the fully qualified server names. And each conf file is slightly different, so the same pattern of substitutions doesn't work on all the conf files.

I can get close with just a search "[^ ]*.domain.com" in vi, but I don't want to match the names, I want to exclude them, unless I can pull all of the matches out and put them in a new file. However, I could not figure out if you can do that either...

Far better to delete everything but "[^ ]*.domain.com"

Does someone know how to do that in a substitution, or some other command?

Any help appreciated!

Post your input and desired output.

I am not at work, and won't be for a few days...I'll see if I can get a hold of it. In the interium, this should give you an idea.

I think I misquoted the search that worked. I believe it should be:

"[^ ].*.domain.com"

Conf file reads like a typical shell script, with server names in the logic statements.

From memory, the typical area in question goes like:

-------------------------------------------
variable= {

"server1.domain.com", "server2.domain.com",
"server3.domain.com", "server4.domain.com", };

If <something> == server1.domain.com || <something> == server5.domain.com
then <stuff>
else <other stuff> server3.domain.com
fi
----------------------------------------------

That is just a sample, it is usually a couple pages of logic, with server names interspersed.

All I want is a list of servers from that code, which would look like this:

server1.domain.com
server2.domain.com
server3.domain.com
server4.domain.com
server1.domain.com
server5.domain.com
server3.domain.com

Which I can then sort with "cat list | sort -u | sort -n &gt; newer_list" to get a single unique list of servers .

 Basically, just either extracting the server names, queing off the domain name, or delete every but the servernames. I'd make a copy of the file, and use that, of course

Thanks!

This should extract fully quafilied names:

perl -0777 -ne'$,="\n";print /[^ "]+\.domain\.com/g; print "\n"' file

And this only hostnames:

perl -0777 -ne'$,="\n";print /[^ "]+(?=\.domain\.com)/g; print "\n"' file
1 Like

Awesome! I'll give that a try.

Thanks again!