0

The file name is in a format like this:

YYYY-MM-DD_hostname_something.log

I want to get the hostname from the filename. The hostname can be any length, but always has a _ before and after. This is my current awk statement. It worked fine until the hostname length changed. Now I can't use it anymore.

awk 'BEGIN { OFS = "," } FNR == 1 { d = substr(FILENAME, 1, 10) } { h = substr(FILENAME, 12, 10) } $2 ~ /^[AP]M$/ && $3 != "CPU" { print d, $1 "" $2, h, $4+$5, $6, $7+$8+$9}' *_something.log > myfile.log

3 Answers 3

2
echo 'YYYY-MM-DD_hostname_something.log' | awk -F"_" '{print $2}'

Output:

hostname

I suppose your hostname contains no _.

Sign up to request clarification or add additional context in comments.

1 Comment

Try this: find -name "*_something.log" | awk -F"_" '{print $2}'
1
$ ls YYYY-MM-DD_hostname_something.log | cut -d _ -f 2
hostname

The cut(1) utility is POSIX and accepts the -d _ option to specify a delimiter and -f 2 to specify the second field. It has got a few more nifty options that you can read about in its fine manual page.

Comments

0

Since you have mentioned you need to modify your awk code, replace your substr function with split.

split(FILENAME,a,"_");date = a[1];host = a[2] 
  • split the FILENAME value into array a with _ as FS.
  • a[1] will contain date
  • a[2] will contain the hostname value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.