如何通过shell脚本中提取一个字符串的一部分? [英] How can I extract part of a string via a shell script?
本文介绍了如何通过shell脚本中提取一个字符串的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的字符串是设置像这样:
的href =&放大器; QUOT;
的 PART我想提取的&安培; QUOT ;&放大器; GT; [链接]
块引用>解决方案下面是在Bash中另一种方式:
$字符串=HREF =&放大器; QUOT;第二部分我想提取和放大器; QUOT;&放大器; GT; [链接]
$实体=&放大器; QUOT;
$字符串= $ {#字符串* $ {}实体*}
$字符串= $ {字符串%* $ {}实体*}
$回声$字符串
第一部分我想提取这说明了两个特点:删除匹配preFIX /后缀模式和使用一个变量来保存模式(你可以用文字代替)
The string is setup like so:
href="
PART I WANT TO EXTRACT">[link]
解决方案Here's another way in Bash:
$ string="href="PART I WANT TO EXTRACT">[link]" $ entity=""" $ string=${string#*${entity}*} $ string=${string%*${entity}*} $ echo $string PART I WANT TO EXTRACT
This illustrates two features: Remove matching prefix/suffix pattern and the use of a variable to hold the pattern (you could use a literal instead).
这篇关于如何通过shell脚本中提取一个字符串的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文