• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

awk question for a simple regex

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:
4 Solutions
head -1  filename | awk -F"Notes: Host:| Problem:" '{print $2}'
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
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
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
TIMFOX123Author Commented:
Thank you all

this really helps me!!

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now