【发布时间】:2017-01-28 22:38:51
【问题描述】:
已经发布的使用 awk 或 sed 的解决方案是相当标准的,并且在某些事情不能正常工作时提供帮助。
喜欢一个:
StringStr="ValueA:ValueB,ValueC:ValueC" ;
echo ${StringStr} | gawk -F',' 'BEGIN{}{for(intx=1;intx<=NF;intx++){printf("%s\n",$(intx))}}END{}'
确实会产生相同的结果,但是可以登录其帐户并且具有较少选项(例如由于特定原因不允许使用 awk 或 gawk)的受限用户确实必须产生每次都必须工作的东西。
出于有效的原因,我确实在 github.com 上开发了自己的 Bash 函数库,但使用了一种无法按预期工作的技术,这里是一个工作示例:
此技术使用 Bash 的“删除匹配前缀模式”和“删除匹配后缀模式”。目标是获取一串链接信息以使用尽可能简单的 bash-shell 元素来提取插入的元素。
到目前为止,我确实有第一条语句来获取特定格式的字符串: 例如:
StringPattern="__VALUE1__:__VALUE2__,"
格式假设在链中添加多个StringPattern 类型的Pattern。 剩下的 ',' 将用于拆分和分离字符串 VALUE1:VALUE2形式。
像StringStorage一样会保存很多次,解析StringPattern,这里举2个例子: 1 - 样本 1
StringPattern="VariableA:InformationA,"
StringStorage="${StringStorage}${StringPattern}" ;
2 - 样本 2
StringPattern="VariableB:InformationB,"
StringStorage="${StringStorage}${StringPattern}" ;
此时,StringStorage 正确保存了这些信息:
StringStorage="VariableA:InformationA,VariableB:InformationB,"
现在使用 StringStorage,由“删除匹配前缀模式”和“删除匹配后缀模式”混合而成的 bash 算法确实适用于这种情况:
### Description of IntCsvCount
### does remove all chosed Comma Separated value ',' from StringStorage
### and subtract from the original length the removed result from this
### subtraction. This produce IntCsvCount == 2
IntCsvCount=$( cstr=${StringStorage//,/} ; echo $(( ${#StringStorage} - ${#cstr} )) ) ;
### Description of
### Will be re Variable used to put the Extracted sequence.
bstr="" ;
### Description of for
### Received information from IntCsvCount it should count
### from 0 to Last element . This case it's ${IntCsvCount}-1 or 1 in
### my example.
for (( intx=0 ; intx <= ${IntCsvCount}-1 ; intx++ )) ; do
### This extracting First Segment based on
### Remove matching suffix pattern ${parameter%word} where
### work is ${astr#*,} ( Remove matching prefix pattern ) of
### everything in $astr until find a ',' .
bstr=${astr%*${astr#*,}} ;
### Destroying the $bstr part in by starting the astr to position of
### the end of size equivalent of bstr size (${#bstr}), end position is
### equal to [ Highest-String size ] - [ Shortest-String size ]
astr=${astr:${#bstr}:$(( ${#astr} - ${#bstr}))} ;
echo -ne "Element: ${bstr}\n" ;
done
这应该会产生以下答案。
Element: VariableA:InformationA,
Element: VariableB:InformationB,
将其放入函数中只需将 CSV 更改为“:”并提取“VariableA”和“InformationA”。
问题开始使用非统一的字符串。正如在此板上观察到的,句子示例和切割部分应该适用于非均匀字符串,但这里的示例不起作用。而且我确实有不止一个建议可以使用 gawk、sed 甚至 cut,但是从这个算法来看,它不适用于这个示例:
astr="master|ZenityShellEval|Variable declaration|Added Zenity font support to allow choosing both font-name and size and parsing the zenition option, notice --font option require a space between font and size.|20170127|"
来自
astr=$( zenity --width=640 --height=600 --forms --show-header --text="Commit Message" --add-entry="Branch name" --add-entry="function" --add-entry="section" --add-entry="commit Message" --add-calendar="Commit Date" --forms-date-format="%Y%m%d" --separator='|' ) ;
我还强制输出看起来像 StringPattern 应该看起来的样子: asr="${astr}|" ;
除 CSV(逗号分隔值)之外的相同代码已从“,”更改为“|”
IntCsvCount=$( cstr=${astr//|/} ; echo $(( ${#astr} - ${#cstr} )) ) ;
bstr="" ;
for (( intx=0 ; intx <= ${IntCsvCount}-1 ; intx++ )) ; do
bstr=${astr%*${astr#*|}} ;
astr=${astr:${#bstr}:$(( ${#astr} - ${#bstr}))} ;
echo -ne "Element: ${bstr}\n" ;
done
此时输出生成如下输出:
Element:master|ZenityShellEval|Variable declaration|Added Zenity font support to allow choosing both font-name and size and parsing the zenition option, notice --font option require a space between font and size.|20170127|
Element:
Element:
Element:
有什么原因为什么它不应该每次都工作吗?
【问题讨论】:
-
这很难理解。您能否将其简化为您的输入、实际和预期输出以及您尝试获得该结果的代码。
标签: string bash matching text-extraction