在Unix中提取包含在模式和分隔符之间的字符串

时间:2022-09-13 12:24:42

I am trying to parse SQL source or Shell scripts trying to extract object names from it using bash/awk/sed/perl.

我试图解析SQL源或Shell脚本尝试使用bash / awk / sed / perl从中提取对象名称。

Sample Text - Lets say a text file contains commands like below :

示例文本 - 假设文本文件包含如下命令:

CALL DBUSER.Compiled_Object_Name('....parameters...');

My intention is to parse the text file to fetch one line at a time. I am using a Do While in bash to achieve this.

我的目的是解析文本文件以一次获取一行。我在bash中使用Do While来实现这一目标。

Next step is to parse such a line to extract the below string which is the Object name.

下一步是解析这样一行以提取下面的字符串,即对象名称。

DBUSER.Compiled_Object_Name

Here, DBUSER is a repeating pattern, and always delimited by (

这里,DBUSER是一个重复模式,并且始终由(

Please suggest.

请建议。

1 个解决方案

#1


1  

$ cat test.sql 
CALL DBUSER.Compiled_Object_Name('....parameters...');
CALL DBUSER.Object_Name_1('....parameters...');
# WHATEVER
CALL DBUSER.Compiled_Object_Name('....parameters...');

Grep/Egrep:

grep的/ EGREP:

grep -o 'DBUSER\.[^\(]\{1,\}' test.sql
egrep -o 'DBUSER\.[^\(]+' test.sql

Perl:

Perl的:

perl -ne 'next unless /DBUSER/; ($m) = $_ =~ /(DBUSER\.[^\(]+)/; print "$m\n";' test.sql

Result from both:

两者的结果:

DBUSER.Compiled_Object_Name
DBUSER.Object_Name_1
DBUSER.Compiled_Object_Name

#1


1  

$ cat test.sql 
CALL DBUSER.Compiled_Object_Name('....parameters...');
CALL DBUSER.Object_Name_1('....parameters...');
# WHATEVER
CALL DBUSER.Compiled_Object_Name('....parameters...');

Grep/Egrep:

grep的/ EGREP:

grep -o 'DBUSER\.[^\(]\{1,\}' test.sql
egrep -o 'DBUSER\.[^\(]+' test.sql

Perl:

Perl的:

perl -ne 'next unless /DBUSER/; ($m) = $_ =~ /(DBUSER\.[^\(]+)/; print "$m\n";' test.sql

Result from both:

两者的结果:

DBUSER.Compiled_Object_Name
DBUSER.Object_Name_1
DBUSER.Compiled_Object_Name