Cut command with delimiter Control-A

10,592

^A is character number 1 in the ASCII table a.k.a Start of Heading character. If you're using bash, you can have this:

cut -f 2-8 -d $'\x01'

Or use printf (can be builtin or an external binary):

CTRL_A=$(printf '\x01')
cut -f 2-8 -d "$CTRL_A"

You can also verify your output with hexdump:

hexdump -C b.dat
Share:
10,592
blackmamba
Author by

blackmamba

Updated on June 14, 2022

Comments

  • blackmamba
    blackmamba about 2 years

    I am trying to get some columns from file1 to file2 using cut command with delimiter Control A.

    This is what I tried:

    cut -d^A -f2-8 a.dat > b.dat
    

    If my records are like this:

    A^AB^AC^AD^AE^AF^AG^AH^A$
    

    my command gives:

    AB^AC^AD^AE^AF^AG^AH
    

    Is my command wrong or am I putting the delimiter in a wrong way?

    So it leaves Control-A's A in the starting point.

  • chepner
    chepner almost 10 years
    No need to know the ASCII value of Control-A; -d $'\cA' should work as well.
  • user4815162342
    user4815162342 almost 3 years
    The problem with this approach is that the control character might have been chosen precisely because commas (and other non-control characters) can be present in the fields. In other words, if choosing the approach from this answer, make sure that the "more workable" character is absent from the fields it's supposed to separate.