linux - 使用 grep、awk 或 sed 解析字符串

标签 linux bash shell awk sed

我有一个包含如下行的文件

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

我想捕获某些字符串,以便其输出文件如下所示;

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

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

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 指出的扩展正则表达式:

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

关于linux - 使用 grep、awk 或 sed 解析字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39240688/

相关文章:

c++ - 将库添加到 MonoDevelop C++ 项目

python - Python脚本与linux shell的交互

linux - 获取每个ip地址/ifconfig接口(interface)命令Linux的前3行

linux - Linux Shell脚本中for循环的语法

linux - 删除公共(public)子目录

bash - 错误 : must use subscript when assigning associative array

Linux 哈希命令搜索

linux - 从命令行修改文本文件中的特定行

Linux ELF/LSB 二进制调试器

c - libcurl 出现编译错误