`cut` using a string as the delimiter?

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
asked Mar 18, 2014 by Eric Z Ma (44,280 points)

1 Answer

Best answer

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.

answered Mar 18, 2014 by Eric Z Ma (44,280 points)
edited Jul 18, 2017 by Eric Z Ma
How to cut by multiple spaces in Linux? asked Oct 16, 2015 by anonymous

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost