Reputation: 433
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
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
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]
Upvotes: 0
Reputation: 88989
echo 'YYYY-MM-DD_hostname_something.log' | awk -F"_" '{print $2}'
Output:
hostname
I suppose your hostname contains no _
.
Upvotes: 2