• 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:
 Problem:

so in this case it would find:
bart
0
TIMFOX123
Asked:
TIMFOX123
4 Solutions
 
woolmilkporcCommented:
head -1  filename | awk -F"Notes: Host:| Problem:" '{print $2}'
0
 
amitnepalCommented:
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
0
 
woolmilkporcCommented:
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
0
 
simon3270Commented:
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
0
 
TIMFOX123Author Commented:
Thank you all

this really helps me!!
0

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