0

我需要一些关于是否可以完成的建议..我有以下数据(这只是一行,会有这样的行块)在运行命令后出现,有没有办法可以使用 grep 和 awk 并解析每一行都可以一次性获取号码和所有者电子邮件,如下所示

Output:-

12345 mbarry@int.qualcomm.com

Input:-
change I5e55796844350e543f8460c53ec6e755ebe663d4
  project: platform/vendor/company-proprietary/chip
  branch: master
  id: I5e55796844350e543f8460c53ec6e755ebe663d4
  number: 12345
  subject: chip: changes to tl logging structure
  owner:
    name: Gord barry
    email: mbarry@int.qualcomm.com
    username: mbarry
  url: https://review-android.quicinc.com/12345
  commitMessage: chip: changes to tl logging structure

                 The existing TL logging has been divided into three distinct modules:
                 TL_BA (14), TL_HO (13) and TL (existing module). Thus the log with
                 loglevel 5 in file chip_qct_tl_hostsupport.c can be viewed by issuing
                 the following command - iwpriv chip0 setchipdbg 13 5 1.

                 Change-Id: I5e55796844350e543f8460c53ec6e755ebe663d4
  createdOn: 2012-08-09 15:40:57 PDT
  lastUpdated: 2012-08-21 16:43:08 PDT
  sortKey: 001f390f00023ead
  open: true
  status: NEW
  currentPatchSet:
    number: 3
    revision: 922872178946a712ab9f04483bc93216573cec6e
    parents:
 [ae259408e6ab530be62e02fdeafef34834d68709]
    ref: refs/changes/17/12345/3
    uploader:
      name: Gord barry
      email: mbarry@int.qualcomm.com
      username: mbarry
    createdOn: 2012-08-21 16:43:08 PDT
    files:
      file: /COMMIT_MSG
      type: ADDED
    files:
      file: rich/CORE/TL/inc/tlDebug.h
      type: MODIFIED
    files:
      file: rich/CORE/TL/inc/chip_qct_tl.h
      type: MODIFIED
    files:
      file: rich/CORE/TL/src/chip_qct_tl.c
      type: MODIFIED
    files:
      file: rich/CORE/TL/src/chip_qct_tl_ba.c
      type: MODIFIED
    files:
      file: rich/CORE/TL/src/chip_qct_tl_hosupport.c
      type: MODIFIED
    files:
      file: rich/CORE/VOSS/inc/vos_types.h
      type: MODIFIED
    files:
      file: rich/CORE/VOSS/src/vos_trace.c
      type: MODIFIED
    files:
      file: rich/CORE/WDA/src/chip_qct_wda_ds.c
      type: MODIFIED
    files:
      file: rich/CORE/WDI/TRP/DTS/src/chip_qct_wdi_dts.c
      type: MODIFIED
4

2 回答 2

3

grep一次性使用:

$ grep -Po '(?<=(email|umber): )\S+' file
12345 
mbarry@int.qualcomm.com
3 
mbarry@int.qualcomm.com

用于xargs -n2将两者放在一条线上:

$ grep -Po '(?<=(email|umber): )\S+' file | xargs -n2
12345 mbarry@int.qualcomm.com
3 mbarry@int.qualcomm.com

$ grep -Po '(?<=(email|umber): )\S+' tfile | paste - -
12345   mbarry@int.qualcomm.com
3       mbarry@int.qualcomm.com

解释:

这是匹配后跟的正向回溯 。在您的情况下,您希望匹配后面的字符串,或者后面的正面外观必须是固定长度,因此我们必须删除数字。匹配一个或多个非空白字符。'(?<=a)b'baemail:number:n\S+

(?<=   # Positive lookbehind 
(      # Group for alternation
email  # Literal string email
|      # Alternation (or)
umber  # Literal string umber
)      # Close 
:      # : Literal colon and single space 
)      # Close positive lookbehind 
\S+    # One or more non-whitespace character

awk

$ awk -F: '/email|number/{print $2}' file | xargs -n2
12345 mbarry@int.qualcomm.com
3 mbarry@int.qualcomm.com
于 2013-01-03T21:09:22.980 回答
1

试试这些:

awk -F'[[:space:]:]+' '{a[$2]=$3} END{ print a["number"], a["email"] }' file
awk -F'[[:space:]:]+' '{a[$2]=$3} /email:/{ print a["number"], a["email"] }' file
awk -F'[[:space:]:]+' '{a[$2]=$3} /email:/{ print a["number"], a["email"]; exit }' file

如果这些都不是您要查找的内容,请提供有关您要查找的内容的更多详细信息。

以下是上面最后一个脚本对我发布的示例输入的工作方式:

$ head -15 file
change I5e55796844350e543f8460c53ec6e755ebe663d4
  project: platform/vendor/company-proprietary/chip
  branch: master
  id: I5e55796844350e543f8460c53ec6e755ebe663d4
  number: 12345
  subject: chip: changes to tl logging structure
  owner:
    name: Gord barry
    email: mbarry@int.qualcomm.com
    username: mbarry
  url: https://review-android.quicinc.com/12345
  commitMessage: chip: changes to tl logging structure

                 The existing TL logging has been divided into three distinct modules:
                 TL_BA (14), TL_HO (13) and TL (existing module). Thus the log with

$ awk -F'[[:space:]:]+' '{a[$2]=$3} /email:/{ print a["number"], a["email"]; exit }' file
12345 mbarry@int.qualcomm.com
于 2013-01-03T20:57:16.960 回答