Search code examples
linuxcut

Empty string as a output field seperator for Cut


How can I use cut with --output-delimiter=""? I want to join two columns using cut.

I tried the following command. However cat -v shows that there are non printable characters. Specifically "^@". Any suggestions to how can I overcome this?

cut -d, -f 3,6 --output-delimiter=""  file1.csv | cat -v

This is the content of my file

011,IBM,Palmisano,t,t,t
012,INTC,Otellini,t,t,t
013,SAP,Snabe,t,t,t
014,VMW,Maritz,t,t,t
015,ORCL,Ellison,t,t,t
017,RHT,Whitehurst,t,t,t



When i run my command I'm seeing
Palmisano^@t
Otellini^@t
Snabe^@t
Maritz^@t
Ellison^@t
Whitehurst^@t

Expected output: Basically I want to exclude ^@ in the output

Palmisanot
Otellinit
Snabet
Maritzt
Ellisont
Whitehurstt

Thank you.


Solution

  • The output delimiter is not an empty string, but probably the NULL character. You might want to try

    cut -d, -f 3,6 --output-delimiter=$'\00' file1.csv
    

    (Assuming your shell supports $'...'-quoting; bash and zsh are fine here, not sure about others).

    edit:

    cut apparently puts the NULL character if the output separator is set to the empty string. I do not see a way around it.

    If awk is an acceptable solution, this will do the trick:

    awk -F, '{print $3 $6}' file*
    

    If you want to be more verbose and explicit:

    awk 'BEGIN{FS=","; OFS=""}; {print $3,$6}' file*
    

    FS="," sets the field separator to ,. OFS="" sets the Output Field Separator to the empty string.