简体   繁体   中英

Regex to extract nth element from log entry

The log data I'm working on is in the following format:

FORMAT 1:

2015-01-03T18:52:06Z Sat, 03 Jan 2015 18:52:05 GMT GET /images/title2.png 304  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.10 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.10 Dublin

Fields in the above entry are "space" separated.

FORMAT 2:

2015-01-03T18:52:06Z|Sat, 03 Jan 2015 18:52:05|GMT|GET|/images/title2.png 304| Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.10 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.10|Dublin

Fields in the above entry are "vertical pipe line" separated.

The problem: since the number of fields doesn't change, I'm looking to create a regex to extract a field that occurs AFTER the Nth number of "vertical pipe lines" or Nth number of "spaces" all the way to the next encounter of a "vertical pipe line" or a "space".

A good job for :

awk -F'[ |]' '{print $9}' file

where $9 is the 9th element (separated by spaces or | )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM