String 一个纯Bash剪切脚本,不能提供有效的工作

String 一个纯Bash剪切脚本,不能提供有效的工作,string,bash,matching,text-extraction,String,Bash,Matching,Text Extraction,已经发布的使用awk或sed的解决方案是相当标准的,并且在某些东西不能正常工作时提供帮助 例如: StringStr="ValueA:ValueB,ValueC:ValueC" ; echo ${StringStr} | gawk -F',' 'BEGIN{}{for(intx=1;intx<=NF;intx++){printf("%s\n",$(intx))}}END{}' 该格式假设在链中添加StringPattern类型的多个模式。 剩余的“,”将用于拆分字符串并将其重新分开

已经发布的使用awk或sed的解决方案是相当标准的,并且在某些东西不能正常工作时提供帮助

例如:

StringStr="ValueA:ValueB,ValueC:ValueC" ; 

echo ${StringStr} | gawk -F',' 'BEGIN{}{for(intx=1;intx<=NF;intx++){printf("%s\n",$(intx))}}END{}'
该格式假设在链中添加StringPattern类型的多个模式。 剩余的“,”将用于拆分字符串并将其重新分开 值1值2表单

与StringStorage一样,经过解析的StringPattern会多次保存,这里有两个示例: 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
将其放入函数只需通过“:”更改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应该的样子: astr=“${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

为什么每次都不能用呢?

因此,您发布了以下AWK脚本:

BEGIN{}{for(intx=1;intx<=NF;intx++){printf("%s\n",$(intx))}}END{}

如果我正确理解了问题的结尾,那么您有一个字符串,如
astr=“master | ZenityShellEval | Variable declaration”|添加了Zenity字体支持,以允许选择字体名称和大小并解析zenition选项,请注意--font选项需要字体和大小之间的空格。| 20170127 |”
,您需要以下输出:

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:
Element: master
Element: ZenityShellEval
Element: Variable declaration
Element: 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.
Element: 20170127
我能想到的最简单的方法是:

s="${astr%|}"; echo "Element: ${s//|/$'\n'Element: }";
另外,不要忘记阵列!我想它们会对你的工作有用的。以下操作也会产生所需的输出:

(IFS='|'; declare -a a=(${astr}); printf "Element: %s\n" "${a[@]}")

,我建议您仔细查看。

以下是最后几个主题的相同运行:

IFS="|" read -ra arr<<<"${astr}"
printf "Element: %s\n" "${arr[@]}"
当然,当前解决方案的awk:

awk 'NF && $0 = "Element: " $0' RS="|" <<<"$astr"

这很难理解。您能否将其简化为您的输入、实际输出和预期输出,以及您尝试获得该结果的代码。据我所知,awk脚本是一种替代方案,用于为web提供商计算来自iptable的流量。但如前所述,这只是一种替代方案,事实上,它是在不到两行中保留一个有效的解决方案:
bstr=${astr%*${astr*};astr=${astr:${bstr}:$(${astr}-${bstr}))。这个解决方案可以通过CSV一次弹出最右边的元素,直到astr中的字符串变为空。它效率高,每次迭代需要更少的cpu周期。我应该在我的github Fnct.D中发布一些东西,这是一种快速开发和清晰的努力。保留
bstr=${astr%*${astr*}最重要的两件事;astr=${astr:${bstr}:$(${astr}-${bstr})),终于开始工作了,我认为这是内存的问题,一旦问题解决了,它就会恢复工作,所以要么没有问题,要么共享一个方法,以更少的声明和更少的工作来有效地提取最左边的CSV。对于数组,我确实喜欢使用
declare-array=(${astr/|/})按空间扩展CSV,数组必须将其拆分为元素。需要为${Array[@]}中的项添加
;不回显${item};完成
,我们在其中添加了3行,效率低下。
(IFS='|'; declare -a a=(${astr}); printf "Element: %s\n" "${a[@]}")
IFS="|" read -ra arr<<<"${astr}"
printf "Element: %s\n" "${arr[@]}"
echo -n "ValueA:ValueB,ValueC:ValueC" | awk '1' RS=","
awk 'NF && $0 = "Element: " $0' RS="|" <<<"$astr"