简体   繁体   English

用sed提取单引号之间的字符串

[英]Extract string between single quotes with sed

I have a thousand Delphi files (.pas), and I need to extract text from them. 我有一千个Delphi文件(.pas),我需要从中提取文本。

The text I need is between single quotes (Pascal strings), and I only need the strings called from a particular function. 我需要的文本在单引号(Pascal字符串)之间,并且我只需要从特定函数调用的字符串。 Eg: my_function('This is the string I need') 例如:my_function('这是我需要的字符串')

I have extracted all the lines that appear the function and added to a text file, using find and grep, but I'm unable to extract the strings. 我已经使用find和grep提取了出现该函数的所有行并将其添加到文本文件中,但是无法提取字符串。

I've been looking around the Internet for a regex to extract this strings, but I don't know how to do this. 我一直在互联网上寻找正则表达式来提取此字符串,但是我不知道该怎么做。 I'm trying with this: 我正在尝试与此:

sed "s/.*my_function\('(.*)'\).*/\1/" all_the_strings.txt > my_out_file.txt

But it doesn't work (I'm not an expert with regex...). 但这是行不通的(我不是regex的专家...)。

Can you help me with this? 你能帮我吗?

这可能对您有用(GNU sed):

sed -nr "s/.*my_function\('([^']*)'\).*/\1/p" all_the_strings.txt > my_out_file.txt

您可以尝试以下方法:

sed 's/.*my_function(.\(.*\).).*/\1/;'

Your solution doesn't escape parentheses at right place. 您的解决方案无法在正确的位置转义括号。 In they are not special metacharacters, so they match literal. 它们不是特殊的元字符,因此它们与文字匹配。

You must escape them to do grouping, so change the regexp to escape the internal ones, like: 您必须对它们进行转义以进行分组,因此请更改正则表达式以转义内部的正则表达式,例如:

sed "s/.*my_function('\(.*\)').*/\1/" all_the_strings.txt > my_out_file.txt

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

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