简体   繁体   English

如何在Linux中使用SED regex从一行中提取多个字符串并将其写入文件?

[英]How to extract multiple strings from a line using SED regex in Linux and write them to a file?

I have an XML file with multiple lines like below ( I only care about the lines that start with SOURCE) 我有一个包含多行的XML文件,如下所示(我只关心以SOURCE开头的行)

SOURCE BUSINESSNAME ="" DATABASETYPE ="Oracle" DBDNAME ="OrclExp11g" DESCRIPTION ="" NAME ="EMPLOYEES" OBJECTVERSION ="1" 

SOURCE BUSINESSNAME ="" DATABASETYPE ="Oracle" DBDNAME ="OrclExp11g" DESCRIPTION ="" NAME ="HR" OBJECTVERSION ="1"

In every line that starts with SOURCE I need to get 3 strings and write them to another file like below. 在以SOURCE开头的每一行中,我需要获取3个字符串并将它们写入另一个文件,如下所示。

Oracle,OrclExp11g,EMPLOYEES Oracle,OrclExp11g,员工

Oracle,OrclExp11g,HR 甲骨文,OrclExp11g,HR

sed -n -e '/SOURCE /p' InputFile.XML | sed -r 's/.* NAME \=\"(.+)\" OBJECTVERSION \=\".*/\1/'  > $Source_List.Out

I am new to using SED but so far I was able to get out only one string out using SED. 我是使用SED的新手,但到目前为止,使用SED只能得到一个字符串。 I really appreciate if anyone can help me how to get 3 strings out. 如果有人可以帮助我如何获得3个字符串,我非常感谢。 Thanks so much in advance! 非常感谢!

As you guessed sed is your friend, you could replace matched regex using \\1 , \\2 and so on. 正如您猜sed是您的朋友一样,您可以使用\\1\\2等替换匹配的正则表达式。

$ sed -nE '/SOURCE/{s/^.*DATABASETYPE ="([^"]*)".*DBDNAME ="([^"]*)".*NAME ="([^"]*)".*$/\1,\2,\3/;p}' file >outputfile

Output 输出量

$ cat outputfile
Oracle,OrclExp11g,EMPLOYEES
Oracle,OrclExp11g,HR

Notes 笔记

  • -E enable extended regex. -E启用扩展的正则表达式。
  • -n with sed suppresses the normal output. -n with sed禁止正常输出。 Only the lines that you would print with p will be printed. 仅将打印用p打印的行。

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

相关问题 使用 sed/awk 从每一行中提取多个字符串 - Using sed/awk to extract multiple strings from each line 正则表达式从文件中提取多行 - Regex extract multiple line from file 在Linux中使用sed从日志文件中提取行 - Using sed in Linux to extract lines from a log file 使用linux实用程序从文件中提取正则表达式。 在单独的一行上打印每个匹配项 - Extract regex from file using linux utility. print each match on a seperate line 如何在给出行范围时使用sed命令从文本文件中提取内容? - How to extract content from a text file using sed command when line ranges are given? 如何使用linux中的sed将包含一个字符串的行中的特定记录替换为另一个文件中的数字 - how to replace specific record on a line containg a string with a number from another file using inplace editing sed in linux 使用 linux 或 python 从文件中提取特定的列和字符串 - Extract the specific columns and strings from a file using linux or python 如何使用 sed 或 awk 从字符串中提取多个参数 - How to extract multiple params from string using sed or awk 如何从Linux中两个相似字符串之间的文件中提取文本? - How to extract the text from a file between two similar strings in linux? 在这种情况下,如何从linux中的文件中的字符串中提取数据? - How to extract data from strings in file in this case, in linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM