#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    1
    Rep Power
    0

    awk/sed solution


    I need a solution to the following:

    I have a file with hostnames listed i.e.

    hostname1
    hsotname2
    hostname3

    I also have another file that will contain one of the above hostnames in the file on any given line.

    The problem is using breaking the awk statement to read the second file for a search match.

    For example:

    cd $systemPath
    ls | awk -F. '{ print $1 }' \
    > /tmp/hostList

    Gives me a file listing of hostnames

    Now I need to search for each hostname
    in a different file, until I find the line containing one of the hostnames. There will be only one, but it will be amongst other fields, probably can use 'split' to create an array and get the hostname.

    This won't work, but maybe you can see what I'm trying to do.

    awk ' /$hostname/ { \
    hostname = split($0, hostLine, " ")
    for (i = 1; i <= z; ++i) {
    print i, hostLine[i]
    }
    }' otherFile > hostFile(contains match)


    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    137
    Rep Power
    0
    Hi,

    Your subject header asks for a sed/awk sollution, so my way of solving this might not be what you want........

    You could do something like:
    Code:
    #!/bin/bash
    
    HOST_NAME_FILE="/path/to/hostnames_file"
    
    for THIS_HOST in `cat ${HOST_NAME_FILE}`
    do
      echo "${THIS_HOST}: $(grep -h ${THIS_HOST} * )"
    done
    This code will produce lines that look like:

    hostname1: word word hostname1 word word
    hostname2: word hostname2 word word

IMN logo majestic logo threadwatch logo seochat tools logo