How to `cut` a String Using a String as the Delimiter in Bash?

Is is possible to cut in Linux using a string as the delimiter?

cut --delimiter="delim" -f 1,3

cut reports:

cut: the delimiter must be a single character

The most closest solution that I find is using awk/gawk:

awk -F 'delim' '{print $1; print $3}'

From the manual:

-F fs
–field-separator fs Use fs for the input field separator (the value of the FS predefined variable).

An you can also use regular expression for the delimiter (field separator):

Similarly, if the FPAT variable is set to a string representing a
regular expression, each field is made up of text that matches that
regular expression. In this case, the regular expression describes the
fields themselves, instead of the text that separates the fields.
Assigning a new value to FS or FIELDWIDTHS overrides the use of FPAT.



Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

One comment:

Leave a Reply

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