在Shell中匹配模式后提取字符串

时间:2022-09-13 00:23:22

How to extract whatever string comes after a matched pattern in Shell script. I know this functionality in Perl scripting, but i dont know in Shell scripting.

如何提取Shell脚本中匹配模式之后的任何字符串。我在Perl脚本中知道这个功能,但在Shell脚本中不知道。

Following is the example,

下面是这个例子中,

Subject_01: This is a sample subject and this may vary

Subject_01:这是一个样本主题,可能会有所不同

I have to extract whatever string that follows "Subject_01:"

我必须提取"Subject_01 "后面的任何字符串"

Any help please.

任何帮助请。

1 个解决方案

#1


3  

It depends on your shell.

这取决于你的壳层。

If you're using shell or or (I believe) , then you can do fancy stuff like this:

如果你使用的是伯恩壳牌或bash或pdksh,那么你可以做一些像这样的花哨的事情:

$ string="Subject_01: This is a sample subject and this may vary"
$ output="${string#*: }"
$ echo $output
This is a sample subject and this may vary
$

Note that this is pretty limited in terms of format. The line above requires that you have ONE space after your colon. If you have more, it will pad the beginning of $output.

请注意,这在格式上是相当有限的。上面的线要求在冒号之后有一个空格。如果你有更多的钱,它将使你的产出开始增加。

If you're using some other shell, you may have to do something like this, with the cut command:

如果您正在使用其他shell,那么您可能需要使用cut命令:

> setenv string "Subject_01: This is a sample subject and this may vary"
> setenv output "`echo '$string' | cut -d: -f2`"
> echo $output
This is a sample subject and this may vary
> setenv output "`echo '$string' | sed 's/^[^:]*: *//'`"
> echo $output
This is a sample subject and this may vary
> 

The first example uses cut, which is very small and simple. The second example uses sed, which can do far more, but is a (very) little heavier in terms of CPU.

第一个例子使用cut,它非常小和简单。第二个示例使用sed,它可以做更多的事情,但是就CPU而言它(非常)有点重。

YMMV. There's probably a better way to handle this in csh (my second example uses tcsh), but I do most of my shell programming in Bourne.

YMMV。在csh中可能有更好的方法来处理这个问题(我的第二个示例使用tcsh),但是我大部分的shell编程都是在Bourne中完成的。

#1


3  

It depends on your shell.

这取决于你的壳层。

If you're using shell or or (I believe) , then you can do fancy stuff like this:

如果你使用的是伯恩壳牌或bash或pdksh,那么你可以做一些像这样的花哨的事情:

$ string="Subject_01: This is a sample subject and this may vary"
$ output="${string#*: }"
$ echo $output
This is a sample subject and this may vary
$

Note that this is pretty limited in terms of format. The line above requires that you have ONE space after your colon. If you have more, it will pad the beginning of $output.

请注意,这在格式上是相当有限的。上面的线要求在冒号之后有一个空格。如果你有更多的钱,它将使你的产出开始增加。

If you're using some other shell, you may have to do something like this, with the cut command:

如果您正在使用其他shell,那么您可能需要使用cut命令:

> setenv string "Subject_01: This is a sample subject and this may vary"
> setenv output "`echo '$string' | cut -d: -f2`"
> echo $output
This is a sample subject and this may vary
> setenv output "`echo '$string' | sed 's/^[^:]*: *//'`"
> echo $output
This is a sample subject and this may vary
> 

The first example uses cut, which is very small and simple. The second example uses sed, which can do far more, but is a (very) little heavier in terms of CPU.

第一个例子使用cut,它非常小和简单。第二个示例使用sed,它可以做更多的事情,但是就CPU而言它(非常)有点重。

YMMV. There's probably a better way to handle this in csh (my second example uses tcsh), but I do most of my shell programming in Bourne.

YMMV。在csh中可能有更好的方法来处理这个问题(我的第二个示例使用tcsh),但是我大部分的shell编程都是在Bourne中完成的。