find string(s) in text file and nearby data, export to list help

Hi,

So I'm kinda new to shell scripts and the like. I've picked up quite a bit of use from browsing the forums here but ran into a new one that I can't seem to find an answer for.

I'm looking to parse/find a string AND the next 15 or so charachters that follow the string within a text file the export it to a text file, csv or whatever. I can sucessfully search for the string, and export it but am stumped :wall: on how to have it grab what is (dynamically) next to the string.

Essentially I have several thousand syslog text files that I want to parse for the word "hostname" but I don't really care about the word "hostname" only the data next to it which is actually the hostname, if that makes sense...and then export what is found to a separate file.

Even a script that if finding the word "hostname" copied the entire line to the new file would be useful....

Thoughts for the n00b?

Try:

perl -nle 'print $& if /hostname.{15}/' logfile > outfile