[英]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 #
#2
( s///
) replaces its content by regular expression #2
( s///
)用正则表达式替换其内容 #3
( p
) prints result #3
( p
)打印结果 #4
( q
) stops processing of file because we already find what we are looking for #4
( q
)停止处理文件,因为我们已经找到了要查找的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.