Path: ...!news.snarked.org!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail From: gazelle@shell.xmission.com (Kenny McCormack) Newsgroups: comp.unix.shell Subject: Splitting in shell (bash) Date: Sat, 9 Nov 2024 16:19:17 -0000 (UTC) Organization: The official candy of the new Millennium Message-ID: Injection-Date: Sat, 9 Nov 2024 16:19:17 -0000 (UTC) Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4"; logging-data="296282"; mail-complaints-to="abuse@xmission.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: gazelle@shell.xmission.com (Kenny McCormack) Bytes: 2514 Lines: 41 There is a feature that is prominently missing from the shell language (I am speaking primarily of bash here) - which is the ability to split a string on a delimiter. This is a common operation in most other text-processing oriented languages (AWK, Perl, etc). First note/caveat: I'm not interested in any solution involving IFS, for two reasons: 1) IFS-based solutions never work for me. 2) Changing IFS is inherently dangerous, because, well, IFS itself in inherently dangerous. Yes, I know it has been somewhat de-fanged recently - but it is still dangerous. Anyway, the point of this thread is that I have recently developed a good solution for this, using bash's "mapfile" command. Suppose we have a string in a variable (foo) like: foo;bar;bletch I.e., with ; as the delimiter. This works well, with a couple of caveats: mapfile -td ';' <<< "$foo" Caveats: 1) You can only have one, single character delimiter. It'd be nice if you could have a reg-exp, like in GAWK. 2) If the output you're processing comes from a process, as is usually the case, special care must be taken: mapfile -td ';' < <(someprocess | awk 1 ORS=) The point is that since ';' is now the delimiter, the newline at the end of the line coming from someprocess is not treated as a delimiter, so it must be disposed of with the AWK script. It'd be nice if you could make both ';' and '\n' be recognized as delimiters. But other than those two caveats, it works well. -- The randomly chosen signature file that would have appeared here is more than 4 lines long. As such, it violates one or more Usenet RFCs. In order to remain in compliance with said RFCs, the actual sig can be found at the following URL: http://user.xmission.com/~gazelle/Sigs/Cancer