使用grep,awk或sed解析字符串 [英] parsing strings using either grep,awk or sed

查看:103
本文介绍了使用grep,awk或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

由于作业ID的长度是可变的,因此我考虑使用正则表达式\ d +并将作业"一词用作字段分隔符将行分成两半,但是我不确定是否可以合并以下内容;

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;

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

推荐答案

sed 版本:

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

或@spasic指出的扩展正则表达式:

or with extended regex as pointed out by @spasic:

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

这篇关于使用grep,awk或sed解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆