0

I have a file which contains several columns separated by special delimiter with multiple chracters like this: <xx>.

A line will look like this:

firststring<xx>11<xx>thirdstring/<xx>22<xx>00<xx>00<xx><xx><xx><xx>-1<xx>-1<xx>1

Some columns does not contain anything, that's why I have some columns like: <xx><xx>

I need to print the 8th and 9th columns of all rows. How to do this? I tried this command but it does not print anything.

cat myfile.txt | cut -d '<xx>' -f8,9
cut: the delimiter must be a single character
Try 'cut --help' for more information.

1 Answer 1

0

Most implementations of awk allow multi-character field separators. So you can try:

awk -F'<xx>' '{print $8,$9}' myfile.txt

If you want to preserve the original separators, then you will need to set the output field separator OFS as well, for example

awk -F'<xx>' 'BEGIN{OFS=FS} {print $8,$9}' myfile.txt

Alternatively, there's Miller if you want a more cut-like syntax:

mlr --nidx --ifs '<xx>' cut -f 8,9 myfile.txt

or, preserving the custom separator

mlr --nidx --fs '<xx>' cut -f 8,9 myfile.txt
2
  • If I will use the first one, how will it separate them? Can I print them with their original separator <xx>
    – user387755
    Commented Dec 26, 2019 at 2:26
  • @user12591981 please see updated answer Commented Dec 26, 2019 at 2:35

You must log in to answer this question.