Results 1 to 5 of 5

Thread: Bash: delete characters after colon in specific columns

  1. #1
    Join Date
    Jan 2014
    Beans
    17

    Question Bash: delete characters after colon in specific columns

    Hello !

    I have this tab-delimited file :
    Code:
    KE339719.1    13    0/0:2,0:2:33:0,33,428       0/0:1,0:1:9:0,9,148
    KE332739.1    14    md                          0/0:3,1:4:15:0,15,276
    KE336330.1    14    0/1:5,12:17:99:157,0,382    0/1:2,7:9:99:100,0,147
    In the third and fourth column, I want to delete everything that is after ":", to obtain :
    Code:
    KE339719.1    13    0/0    0/0
    KE332739.1    14    md     0/0
    KE336330.1    14    0/1    0/1
    I wanted to combine awk and sed but don't manage to find a solution

    Any help ?

    Thanks !

    Muriel

  2. #2
    Join Date
    Feb 2008
    Location
    Funky Town, AUS
    Beans
    Hidden!
    Distro
    Xubuntu

    Re: Bash: delete characters after colon in specific columns

    Thread moved to Programming Talk.

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Bash: delete characters after colon in specific columns

    This is how you do it in Bash:
    Code:
    while read -r f1 f2 f3 f4
    do
        printf '%s\t' "$f1" "$f2" "${f3%%:*}" "${f4%%:*}"
        printf '\n'
    done < file
    See: BashFAQ 001 (link in my sig) and
    http://mywiki.wooledge.org/BashGuide...eter_Expansion

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash: delete characters after colon in specific columns

    Code:
    sed -r 's/:[^ \t]+//g'
    though it won't necessarily clean up the formatting

    Code:
    while read -r c1 c2 c3 c4; do printf "%s\t%s\t%s\t%s\n" "$c1" "$c2" "${c3%%:*}" "${c4%%:*}"; done < file

    Code:
    $ sed -r 's/:[^ \t]+//g' tab_delim.txt 
    KE339719.1    13    0/0       0/0
    KE332739.1    14    md                          0/0
    KE336330.1    14    0/1    0/1
    $ while read -r c1 c2 c3 c4; do printf "%s\t%s\t%s\t%s\n" "$c1" "$c2" "${c3%%:*}" "${c4%%:*}"; done < tab_delim.txt 
    KE339719.1	13	0/0	0/0
    KE332739.1	14	md	0/0
    KE336330.1	14	0/1	0/1
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Jan 2014
    Beans
    17

    Re: Bash: delete characters after colon in specific columns

    Hello !

    Thank you both for your answers.

    I tried the sed option and don't have formatting problems so it's perfect !

    Mu

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •