Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hoping somebody can teach me how to do this task.

I am thinking awk might be good to do this, but I am really beginner.

I have a file like below (tab separated, actual file is much bigger). Here, important columns are second and ninth (235 and 15 in the first line of the file).

S   235 1365    *   0   *   *   *   15  1   c81 592
H   235 296 99.7    +   0   0   3I296M1066I 14  1   s15018  1
H   235 719 95.4    +   0   0   174D545M820I    15  1   c2664   10
H   235 764 99.1    +   0   0   55I764M546I 15  1   c6519   4
H   235 792 100 +   0   0   180I792M393I    14  1   c407    107
S   236 1365    *   0   *   *   *   15  1   c474    152
H   236 279 95  +   0   0   765I279M321I    10-1    1   s7689   1
H   236 301 99.7    -   0   0   908I301M156I    15  1   s8443   1
H   236 563 95.2    -   0   0   728I563M74I 17  1   c1725   12
H   236 97  97.9    -   0   0   732I97M536I 17  1   s11472  1

I would like to extract lines by specifying the value of ninth columns. At this time, second columns will be like pivot column. What I mean pivot column is, consider as a single set of data if second column has same value. And within the set of lines, all lines need to have the specific values in the ninth column.

So, for example, if I specify ninth column "14" and "15". Then out put will be.

S   235 1365    *   0   *   *   *   15  1   c81 592
H   235 296 99.7    +   0   0   3I296M1066I 14  1   s15018  1
H   235 719 95.4    +   0   0   174D545M820I    15  1   c2664   10
H   235 764 99.1    +   0   0   55I764M546I 15  1   c6519   4
H   235 792 100 +   0   0   180I792M393I    14  1   c407    107

6th and 8th lines have "15" in their ninth column, but other lines in the "set" (specified by second column: 236) have values other than "14" or "15", so I do not want to extract the lines.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
236 views
Welcome To Ask or Share your Answers For Others

1 Answer

$ cat tst.awk
$2 != prevPivot { prtCurrSet() }
$9 !~ /^1[45]$/ { isBadSet=1 }
{ currSet = currSet $0 ORS; prevPivot = $2 }
END { prtCurrSet() }
function prtCurrSet() {
    if ( !isBadSet ) {
        printf "%s", currSet
    }
    currSet = ""
    isBadSet = 0
}

$ awk -f tst.awk file
S   235 1365    *   0   *   *   *   15  1   c81 592
H   235 296 99.7    +   0   0   3I296M1066I 14  1   s15018  1
H   235 719 95.4    +   0   0   174D545M820I    15  1   c2664   10
H   235 764 99.1    +   0   0   55I764M546I 15  1   c6519   4
H   235 792 100 +   0   0   180I792M393I    14  1   c407    107

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...