Search code examples
unixsedcutcsv

How to remove the first column from all files and replace?


I have several tab-delimited files, e.g. the file looks like this:

A213<TAB>foo bar<TAB>sentence
A123<TAB>bar foo<TAB>sentence
B84521<TAB>abc hello<TAB>world
C984<TAB>def word<TAB>hello

I need it to remove the first column and the substitute the tabs with |||, the output should look as such:

foo bar ||| sentence
bar foo ||| sentence
abc hello ||| world
def word ||| hello

I've tried the following but it didn't work:

$ cut -f2,3 file.txt | sed 's/<TAB>/\s|||\s/g'

Solution

  • This can make it:

    $ awk 'BEGIN{FS="\t"; OFS=" ||| "} {print $2, $3}' file
    foo bar ||| sentence
    bar foo ||| sentence
    abc hello ||| world
    def word ||| hello
    

    It is just a matter of defining the input and output field separators accordingly. Then, print second and third fields.

    With cut + sed you could use:

    $ cut -d$'\t' -f2- < file | sed 's/\t/|||/' 
    foo bar|||sentence
    bar foo|||sentence
    abc hello|||world
    def word|||hello
    

    In all cases note you have to indicate what is the field separator.