简体   繁体   English

使用grep,awk或sed解析字符串

[英]parsing strings using either grep,awk or sed

I have a file with lines like below 我有一个带有如下行的文件

17:59:49.987 - JobID 864563: Found 7 clips from SeqID 862753
17:59:49.987 - Processing Job 864562
17:59:50.003 - JobID 864561: Location 14695 applied clip data successfully. Updating OCAMT_GM_Sent
17:59:50.003 - Processing Job 864563
17:59:50.003 - JobID 864564
17:59:50.018 - JobID 864565
17:59:50.034 - Processing Job 864565
17:59:50.034 - JobID 864566
17:59:50.034 - JobID 864562
17:59:50.034 - JobID 864563
17:59:50.034 - Processing Job 864566
17:59:50.049 - JobID 864567
17:59:50.049 - JobID 864564
17:59:50.049 - Trying to send JobID 864566 to location 14623 at http://172.28.48.11/yb/ClipData.php. Retry count 0
17:59:50.049 - Processing Job 864567

I would like to capture certain strings so that its output file is something like below; 我想捕获某些字符串,以便其输出文件如下所示;

864563 17:59:49.987
864562 17:59:49.987
864561 17:59:50.003
864563 17:59:50.003

Since the the job id length is variable I am thinking of using regular expression \\d+ and breaking the line in half using the word Job as a field separator but I am unsure if the following can be combined; 由于作业ID的长度是可变的,因此我考虑使用正则表达式\\ d +并使用单词Job作为字段分隔符将行分成两半,但是我不确定是否可以组合以下内容;

awk -F'Job*' '{print $1}'|awk '{print $1}'
awk -F'Job*' '{print $2}'

from your comments, i assume your are expecting something like this 从您的评论中,我认为您期待这样的事情

using awk 使用awk

awk -F'[ ]+-.*Job(ID)? |:[ ]+|[ ]+' '{print $2, $1}' file

Output: 输出:

864563 17:59:49.987
864562 17:59:49.987
864561 17:59:50.003
864563 17:59:50.003
864564 17:59:50.003
864565 17:59:50.018
864565 17:59:50.034
864566 17:59:50.034
864562 17:59:50.034
864563 17:59:50.034
864566 17:59:50.034
864567 17:59:50.049
864564 17:59:50.049
864566 17:59:50.049
864567 17:59:50.049

sed version: sed版本:

sed -e 's/\([^ ]*\).*Job\(ID\)\? \([0-9]\+\).*/\3 \1/g'

or with extended regex as pointed out by @spasic: 或使用@spasic指出的扩展正则表达式:

sed -E 's/^(\S+).*Job(ID)? ([0-9]+).*/\3 \1/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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