如何在bash中的多字符分隔符上拆分字符串?

前端之家收集整理的这篇文章主要介绍了如何在bash中的多字符分隔符上拆分字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么不能使用以下bash代码
for i in $( echo "emmbbmmaaddsb" | split -t "mm"  )
do
    echo "$i"
done

预期产量:

e
bb
aaddsb
由于您需要换行符,因此您只需使用换行符替换字符串中mm的所有实例即可.在纯粹的原生bash中:
in='emmbbmmaaddsb'
sep='mm'
printf '%s\n' "${in//$sep/$'\n'}"

如果你想在更长的输入流上做这样的替换,你可能最好使用awk,因为bash的内置字符串操作不能很好地扩展到超过几千字节的内容. BashFAQ #21中给出的gsub_literal shell函数(后端到awk)适用:

# Taken from http://mywiki.wooledge.org/BashFAQ/021

# usage: gsub_literal STR REP
# replaces all instances of STR with REP. reads from stdin and writes to stdout.
gsub_literal() {
  # STR cannot be empty
  [[ $1 ]] || return

  # string manip needed to escape '\'s,so awk doesn't expand '\n' and such
  awk -v str="${1//\\/\\\\}" -v rep="${2//\\/\\\\}" '
    # get the length of the search string
    BEGIN {
      len = length(str);
    }

    {
      # empty the output string
      out = "";

      # continue looping while the search string is in the line
      while (i = index($0,str)) {
        # append everything up to the search string,and the replacement string
        out = out substr($0,1,i-1) rep;

        # remove everything up to and including the first instance of the
        # search string from the line
        $0 = substr($0,i + len);
      }

      # append whatever is left
      out = out $0;

      print out;
    }
  '
}

……在这种情况下,用作:

gsub_literal "mm" $'\n' <your-input-file.txt >your-output-file.txt
原文链接:https://www.f2er.com/bash/386937.html

猜你在找的Bash相关文章