简体   繁体   English

使用sed,在文本文件中标识右行,并将三个不同的部分写入以分隔变量

[英]Using sed, identify the right line in text file and write three different portions to separate variables

I have a group of text files that each contain one instance of the following line somewhere, where the "(MR #" is the best way to identify the right line... 我有一组文本文件,每个文本文件在某处包含以下行的一个实例,其中“(MR#”是识别右行的最佳方法...

Smith, John A (MR # MR123456)

I'd like to get three sed statements that, when run against a given file, populate the following three variables: Plname, Pfname and MRnum. 我想获得三个sed语句,当它们针对给定文件运行时,将填充以下三个变量:Plname,Pfname和MRnum。 Using the example above, I'd like the variables to end up holding the following after running the sed statements. 使用上面的示例,我希望变量在运行sed语句后最终保留以下内容。

Plname=Smith
Pfname=John
MRnum=MR123456

Despite many sed examples out there, I'm new to this, and currently struggling with the exact syntax. 尽管有许多sed示例,但我对此并不陌生,并且目前在使用确切的语法。 Thanks for your help. 谢谢你的帮助。

Something like this: 像这样:

$ cat t
bla-bla-bla
Smith, John A (MR # MR123456)
bla-bla-bla
$ Plname="$(sed -n '/(MR #/{s/^\([^,]\+\),.*/\1/p;q}' t)"
$ Pfname="$(sed -n '/(MR #/{s/^[^,]\+,[ ]\?\([^(]\+\).*/\1/p;q}' t)"
$ MRnum="$(sed -n '/(MR #/{s/^[^(]\+(MR # \([^)]\+\).*/\1/p;q}' t)"
$ printf "Plname = %s, Pfname = %s, MRnum = %s\n" "$Plname" "$Pfname" "$MRnum"
Plname = Smith, Pfname = John A , MRnum = MR123456

Little explanation: 小解释:

/(MR #/{s/^\\([^,]\\+\\),.*/\\1/p;q} is a short form of /(MR #/{s/^\\([^,]\\+\\),.*/\\1/p;q}

/(MR #/ #1 { s/^\\([^,]\\+\\),.*/\\1/; #2 p; #3 q #4 }

  • sed will parse file line by line sed将逐行解析文件
  • -n flag says that sed won't print each line (it does it by default), because we will do this manually if needed -n标志表示sed不会打印每行(默认情况下会打印),因为如果需要,我们将手动执行此操作
  • #1 ( // ) finds line which contains (MR # #1// )查找包含(MR #
  • and if line matches then we do following actions: 如果行匹配,那么我们将执行以下操作:
    • #2 ( s/// ) replaces its content by regular expression #2s/// )用正则表达式替换其内容
    • #3 ( p ) prints result #3p )打印结果
    • #4 ( q ) stops processing of file because we already find what we are looking for #4q )停止处理文件,因为我们已经找到了要查找的内容

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

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