如何使用 sed 命令获取特定字符串

mar*_*ark 1 awk grep sed

我有一个如下所示的输入字符串:

   VAL:1|b:2|c:3|VAL:<har:919876543210@abc.com>; tag=vy6r5BpcvQ|VAl:1234|name:mnp|VAL:91987654321
Run Code Online (Sandbox Code Playgroud)

像这样,有1000多行。

我想获取第一个参数的值,即a字段和d字段,但对于d我只想要的字段har:919876543210@abc.com

我试过这样:

cat $filename | grep -v Orig |sed -e 's/['a:','d:']//g' |awk -F'|' -v OFS=',' '{print $1 "," $4}' >> $NGW_DATA_FILE

我得到的输出如下:

1,<har919876543210@abc.com>; tag=vy6r5BpcvQ
Run Code Online (Sandbox Code Playgroud)

我想要这样

1,har:919876543210@abc.com
Run Code Online (Sandbox Code Playgroud)

我在哪里犯了错误,我该如何解决?

Rav*_*h13 5

编辑:根据 OP 对 Input_file 和 OP 评论的更改,现在添加以下内容。

awk '
BEGIN{ FS="|"; OFS="," }
{
  sub(/[^:]*:/,"",$1)
  gsub(/^[^<]*|; .*/,"",$4)
  gsub(/^<|>$/,"",$4)
  print $1,$4
}'  Input_file
Run Code Online (Sandbox Code Playgroud)

使用显示的示例,您能否尝试在 GNU 中使用显示的示例进行以下、编写和测试awk

awk '
BEGIN{
  FS="|"
  OFS=","
}
{
  val=""
  for(i=1;i<=NF;i++){
    split($i,arr,":")
    if(arr[1]=="a" || arr[1]=="d"){
      gsub(/^[^:]*:|; .*/,"",$i)
      gsub(/^<|>$/,"",$i)
      val=(val?val OFS:"")$i
    }
  }
  print val
}
' Input_file
Run Code Online (Sandbox Code Playgroud)

说明:为以上添加详细说明。

awk '                                ##Starting awk program from here.
BEGIN{                               ##Starting BEGIN section of this program from here.
  FS="|"                             ##Setting FS as pipe here.
  OFS=","                            ##Setting OFS as comma here.
}
{
  val=""                             ##Nullify val here(to avoid conflicts of its value later).
  for(i=1;i<=NF;i++){                ##Traversing through all fields here
    split($i,arr,":")                ##Splitting current field into arr with delimiter by :
    if(arr[1]=="a" || arr[1]=="d"){  ##Checking condition if first element of arr is either a OR d
      gsub(/^[^:]*:|; .*/,"",$i)     ##Globally substituting from starting till 1st occurrence of colon OR from semi colon to everything with NULL in $i.
      val=(val?val OFS:"")$i         ##Creating variable val which has current field value and keep adding in it.
    }
  }
  print val                          ##printing val here.
}
' Input_file                         ##Mentioning Input_file name here. 
Run Code Online (Sandbox Code Playgroud)