awk question for a simple regex

Posted on 2012-09-05
Last Modified: 2012-09-10
I am not a good awk-ward guy

I have a file that has error messages in it.   I do a
head -1  filename |

this gives me the the error message.

I want to PICK the hostname out of the string.

I am in a secure environment so I always use "bart" as an example server name.
Notes: Host:bart Problem:

I want to pull out the part of the string that starts with:  
Notes: Host:

and ends with:

so in this case it would find:
Question by:TIMFOX123
    LVL 68

    Assisted Solution

    head -1  filename | awk -F"Notes: Host:| Problem:" '{print $2}'
    LVL 7

    Assisted Solution

    If you just want one line , then woolmilkporc's  answer will work, but if you are looking to get all the hosts with problem , then you would want to do :

    cat filename | awk -F"Notes:Host:| Problem:"'{print$2}'

    If you want to count the number of problem with the same host

     cat filename | awk -F"Notes:Host:| Problem:"'{print$2}' | uniq -c
    LVL 68

    Assisted Solution

    Don't use "cat" if you don't have to!  :-)

    awk -F"Notes:Host:| Problem:"'{print$2}' filename
    awk -F"Notes:Host:| Problem:"'{print$2}' filename | uniq -c
    LVL 19

    Accepted Solution

    You need a "sort" before the "uniq -c" (so end the line "filename | sort | uniq -c"

    Also, this assumes that every line in the file is that format.  If you want only the lines that have this format, you'd need something like:

    awk -F"Notes: Host:| Problem:" '/^Notes: Host:.* Problem:/{print$2}' filename

    Author Closing Comment

    Thank you all

    this really helps me!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
    If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now