How to split a string by string in Bash?

How to split a string by string in Bash? For example,

"a string   separated by space" =>
["a", "string", "separated", "by", "space"]

and

"a,string,separated,by,comma" =>
["a", "string", "separated", "by", "comma"]

You can convert a string to an array using the grammar like

inarr=(${a})

If the delimiter is not space and delimiter is a single character or a string consisting of 1 or more of the characters, set the IFS like

IFS=',' inarr=(${a})

For the examples in the question:

For the space separated string:

$ a="a string   separated by space"
$ inarr=(${a})

Check the result:

$ echo ${inarr[@]}
a string separated by space

For the ‘,’ separated string, set the IFS:

$ a="a,string,separated,by,comma";
$ IFS=',' inarr=(${a});

Check the result:

$ echo ${inarr[@]}
a string separated by comma

Similar Posts

  • Programming language popularity indices?

    Any good programming language popularity indices? Those are interesting ones: TIOBE Indexhttp://www.tiobe.com/index.php/content/paperinfo/tpci/index.html The RedMonk Programming Language Rankings: January 2014 This ranking is published as blog posts. So no persistent homepage found yet. The January 2014 version is: http://redmonk.com/sogrady/2014/01/22/language-rankings-1-14/ Programming Language Popularityhttp://langpop.com/ Read more: Most important aspects or features of the C++ programming language? Are You…

  • Systems Conferences

    Which ones are good systems conferences? Top ones by ACM and USENIX: OSDI: https://www.usenix.org/conferences/byname/179 SOSP: http://sosp.org/ Other SIGOPS Events: http://www.sigops.org/conf-sponsored.html EuroSys: http://www.eurosys.org/ SoCC: http://www.socc2013.org/ (SoCC 2013) ASPLOS: http://www.sigplan.org/Conferences/ASPLOS/Main VEE: http://www.sigplan.org/vee.htm USENIX ATC: https://www.usenix.org/conferences/byname/131 NSDI: https://www.usenix.org/conferences/byname/178 IEEE Conferences: ICDCS: http://www.temple.edu/cis/icdcs2013/ (2013) IPDPS: http://www.ipdps.org/ Other related ones and workshops: HPCA: Search HPCA ConferenceSC: http://www.supercomp.org/IEEE CLUSTER: http://www.clustercomp.org/ HotCloud:…

  • | |

    Installing Xen on Fedora 20 as Domain-0

    I ever introduced [[installing-xen-domain-0-on-fedora-17]] as the first try to use the xen package delivered from Fedora and get away from manually compiling Xen and patching the kernel. In this post, I introduce installing Xen Dom0 on Fedora 20. Installing Xen First, install the xen pacakges: # yum install xen The Linux kernel is already ready…

  • Hadoop TeraSort Benchmark

    TeraSort is one of Hadoop’s widely used benchmarks. Hadoop’s distribution contains both the input generator and sorting implementations: the TeraGen generates the input and TeraSort conducts the sorting. Here, we provide a short tutorial for using the Hadoop TeraSort benchmark. TeraGen generates random data that can be used as input data for a subsequent running…

Leave a Reply

Your email address will not be published. Required fields are marked *