I am looking in a /etc/hosts file for hosts that should contain servers that look at least like this:


and NOT example.com

I want to search with a wildcard like this:

sed/awk/find/grep/ word.word.word < path/to/inputfile > path/to/outputfile where word consists of [A-Z, a-z, 0-9]

Please write 'sed','awk','find' or 'grep' code for this.

closed as unclear what you're asking by doneal24, Wildcard, Jesse_b, mosvy, roaima Oct 19 at 14:18

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Is this a homework question? By "a hosts file" do you mean the /etc/hosts file? Why do you want to search a specific, invalid, way? Did someone give you that guidance? – Jesse_b Oct 18 at 18:35
  • 1
    Your examples are also confusing. Initially I thought you want to find anything that ends with mobile.example.com but given your example search it seems like you want to find anything that is at least 2 subdomains away from a tld? – Jesse_b Oct 18 at 18:39
  • 1
    @Jesse_b thank you for answering. yes, its a "/etc/hosts" file and yes, i only want hosts listed in my outputfile that have at least 3 words like: mobile.example.com and NOT example.com – superbyte Oct 18 at 19:08
  • 2
    edit question to show what you have tried. As you know which file then you don't need find. As you are only searching not modifying then you don't need sed, and awk is more power than you need. So use grep. Read manual, try it, then ask for help, posting what you tried. – ctrl-alt-delor Oct 18 at 19:23
  • 2
    @superbyte: Please read through the comments above, address all the questions and try to understand all the caveats of what you are asking. You haven't really stated any new and valuable information. Your problem is just not as simple as you think it is and almost any attempt we make to solve it will likely either return things you don't want it to, or hide things you don't want it to. – Jesse_b Oct 18 at 20:15

/etc/hosts entries can have one or more hostnames per IP address, so we need to check each hostname (i.e. all fields except the first field) to see if it has 2 or more . characters in it.

For example:

awk '{for (f=2;f<=NF;f++) {if (split($f,array,/\./)>2) {print;last}}}' /etc/hosts

or, with linefeeds and indentation added to improve readability:

awk '
    for (f=2;f<=NF;f++) {
      if (split($f,array,/\./) > 2) {
  }' /etc/hosts

This will print every line in /etc/hosts where any of the hostnames have at least 2 literal . characters.

It iterates over each field in an input line and uses awk's split() function to do this, splitting on . characters. A string split into elements by a delimiter will always have 1 more element than the number of delimiters, so the test has to be > 2 rather than >= 2. e.g. "example" would have one element, itself. "example.com" would have two elements ("example" and "com"). "mobile.example.com" would have 3.

split() also splits the string into an array, but for this task we're only interested in the return value (the number of elements) and ignore the array.

Once the script has found one hostname with two or more . characters, it prints the entire line, breaks out of the for loop (with last), and moves on to the next input line.

  • thank you very much – superbyte Oct 19 at 11:49

Actually, reading your comments, it’s not a real hosts file, but just a list of domin names, so you seem to be satisfied filtering all lines with at least two dots:

grep '\..*\.' hostfile

If it is important that the domain is at the beginning of the line, use

grep '^[[:alnum:]]*\..*\.' hostfile
  • thank you also very much. it gave me the output file i was looking for :-) can you add "-www.example.com" to this grep search ? – superbyte Oct 19 at 11:52
  • The first pattern will also find the address with leading dash, for the second add the dash to the collection: [-[:alnum:]] – Philippos Oct 19 at 12:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.