Find the answer to your Linux question:
Results 1 to 2 of 2
I have a 5-column file: PS 6 15 0 1 PS 1 17 0 1 PS 4 18 0 1 that I would like to get it in this 7-column ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Posts
    9

    Using awk with if/else statement in middle


    I have a 5-column file:

    PS 6 15 0 1
    PS 1 17 0 1
    PS 4 18 0 1

    that I would like to get it in this 7-column format:

    PS.15 PS 6 N 1 0 1
    PS.17 PS 1 P 1 0 1
    PS.18 PS 4 N 1 0 1

    To create 6 of the 7 columns requires just grabbing directly (and sometimes applying small arithmetic) from columns in the original file. However, to create one column (column 4) requires an if-else statement.

    Specifically, to create new columns 1, 2, 3, I use:

    Code:
    cat File | awk '{print $1"."$3"\t"$1"\t"$2}'
    and to create new columns 5, 6,7, I use:

    Code:
    cat testFileB | awk '{print $4+$5"\t"$4/($4+$5)"\t"$5/($4+$5)}'
    and to create new column 4, I use:

    Code:
    cat testFileB | awk '{if ($2 == 1 || $2 == 2 || $2 == 3) print "P"; else print "N";}'
    These three statements work fine independently and get me what I want (the correct values for the columns that are all separated by tabs). However, when I try to apply them simultaneously (create all 7 columns at once), I can only do so with unwanted new lines (instead of tabs) before and after column 4 (the if/else statement column):

    For instance, my attempt to simultaneously create columns 1, 2, 3, 4:

    Code:
    cat File | awk '{print $1"."$3"\t"$1"\t"$2; if ($2 == 1 || $2 == 2 || $2 == 3) print "P"; else print "N";}'
    results in unwanted new lines before column 4:

    PS.15 PS 6
    N
    PS.17 PS 1
    P
    PS.18 PS 4

    Similarly, my attempt to simultaneously create columns 4, 5, 6, 7:

    Code:
    cat File | awk '{if ($2 == 1 || $2 == 2 || $2 == 3) print "P"; else print "N"; print $4+$5"\t"$4/($4+$5)"\t"$5/($4+$5)}'
    results in unwanted new lines after column 4:

    N
    1 0 1
    P
    1 0 1
    N
    1 0 1

    Is there a solution so that I can create all 7 columns at once, and there are only tabs between them (no new lines)?

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    478
    the problem is, awk's "print" statement appends newlines: try using "printf" instead, like this:

    cat File | awk '{printf "%s%s%2d\t%s\t%1d\t",$1,".",$3,$1,$2 ; if ($2 == 1 || $2 == 2 || $2 == 3) print "P"; else print "N";}'
    the sun is new every day (heraclitus)

Posting Permissions

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