user1052448
user1052448

Reputation: 433

How can I get the hostname from a file using awk and regex or substring

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

Upvotes: 0

Views: 362

Answers (3)

Jens
Jens

Reputation: 72756

$ 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.

Upvotes: 1

Mahesh Kharvi
Mahesh Kharvi

Reputation: 399

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.

Upvotes: 0

Cyrus
Cyrus

Reputation: 88989

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

Output:

hostname

I suppose your hostname contains no _.

Upvotes: 2

Related Questions