Using Raku (formerly known as Perl_6)
Briefly, split
destructively removes a separator, which can be defined using a regex pattern:
~$ echo "string1_sep_string2" | raku -ne '.split(/ _sep_ /).put;'
Output:
string1 string2
You can also join
after split
-ting:
~$ echo "string1_sep_string2" | raku -ne '.split(/ _sep_ /).join(",").put;'
Output:
string1,string2
To look at the resultant elements one-per-line, iterate over the output with for
. Adding perl
or raku
to the output call double-quotes the output, indicating that Raku understands the elements to be strings:
~$ echo "string1_sep_string2" | raku -ne '.raku.put for .split(/ _sep_ /);'
Output:
"string1"
"string2"
Note: you'll occasionally run into issues were empty strings remain after split
-ting. These are removed by adding :skip-empty
to the split
call, as in .split(/ _sep_ /, :skip-empty
)`. See examples below.
https://docs.raku.org/routine/split
https://raku.org
sed
,awk
) that are precisely designed for such text processing. But that's for practicality and getting things done. If this is a question of academic interest as I suspect it is, of course, that wouldn't apply.