Neeraj Neeraj - 4 months ago 12
Bash Question

In shell script, can I specify more than one output delimiter in cut?

I have a file with the following data


abcdefghijklmnopqrst


I need an output using command
cut
as the following


bcd|klm.no|pq.rst


I need to include both the delimiters
|
and
.
in my output using cut.

I've got the output with one delimiter using this code

cat file.txt |cut -c2-4,11-13,14-15,16-17,18-20 --output-delimiter='|'


as


bcd|klm|no|pq|rst


Can I get 2 output delimiters by changing something in the command?

Answer

cut supports a single delimiter at a time, but you can easily replace this with a simple sed or Awk script.

awk '{ print substr($0, 2, 4) "|" substr($0, 11, 13) "."
   substr($0, 14, 15) "|" substr($0, 16, 17) "."
    substr($0,18, 20) }' file.txt

Coincidentally the cat is useless.