user10254032
user10254032

Reputation: 193

multiple output from single input based on specific column in awk

I've shoved some of it, here is input.txt

tree one
  output A.txt
  column 2 8642
  column 3
    209.98085 1513.7103
    322.45114 1574.7799
    505.41974 1656.3765
    722.01666 1819.0581
    920.3196 1994.1241
    1192.6161 2156.9763
    1497.4697 2201.7605
    2213.728 2238.4023
    3052.3992 2375.7083
    3618.3948 2490.941
    4548.857 2630.617
    5676.56 2983.299
    7460.303 3430.2625
    12284.128 4114.6753
tree one
  output B.txt
  column 2 11202
  column 3
    207.0211 1509.639
    360.92783 1582.9225
    497.07608 1668.4199
    715.5241 1778.593
    1006.15216 1933.0546
    1136.381 2047.051
    1595.1414 2193.618
    2358.7554 2266.9014
    3052.3992 2375.7083
    4227.2686 2567.7627
    5697.999 3004.2505
    7460.303 3430.2625

By redirect output from awk,

#!/bin/awk -f
/^  output/ {
    output=$2
} 

/^  column 2/{
    two=$3
}

/^    [0-9]/{
    for (i=1; i<=NF; i++) {
    print output,two,$i > output
    }
}

I've tried to sort it horizontally and output into multiple outputs based on a specific column

output A.txt will output all data into A.txt along column 2 and column 3 data

It's working but at some point, it just repeated the output along with two variable, let me know if you have any suggestion.

Actual Result from A.txt

A.txt 8642 209.98085
A.txt 8642 1513.7103
A.txt 8642 322.45114
A.txt 8642 1574.7799
A.txt 8642 505.41974
A.txt 8642 1656.3765
A.txt 8642 722.01666
A.txt 8642 1819.0581
A.txt 8642 920.3196
A.txt 8642 1994.1241
A.txt 8642 1192.6161
A.txt 8642 2156.9763
A.txt 8642 1497.4697
A.txt 8642 2201.7605
A.txt 8642 2213.728
A.txt 8642 2238.4023
A.txt 8642 3052.3992
A.txt 8642 2375.7083
A.txt 8642 3618.3948
A.txt 8642 2490.941
A.txt 8642 4548.857
A.txt 8642 2630.617
A.txt 8642 5676.56
A.txt 8642 2983.299
A.txt 8642 7460.303
A.txt 8642 3430.2625
A.txt 8642 12284.128
A.txt 8642 4114.6753
A.txt 11202 207.0211
A.txt 11202 1509.639
A.txt 11202 360.92783
A.txt 11202 1582.9225
A.txt 11202 497.07608
A.txt 11202 1668.4199
A.txt 11202 715.5241
A.txt 11202 1778.593
A.txt 11202 1006.15216
A.txt 11202 1933.0546
A.txt 11202 1136.381
A.txt 11202 2047.051
A.txt 11202 1595.1414
A.txt 11202 2193.618
A.txt 11202 2358.7554
A.txt 11202 2266.9014
A.txt 11202 3052.3992
A.txt 11202 2375.7083
A.txt 11202 4227.2686
A.txt 11202 2567.7627
A.txt 11202 5697.999
A.txt 11202 3004.2505
A.txt 11202 7460.303
A.txt 11202 3430.2625

Expected output

==> A.txt <==

A.txt 8642 209.98085
A.txt 8642 1513.7103
A.txt 8642 322.45114
A.txt 8642 1574.7799
A.txt 8642 505.41974
A.txt 8642 1656.3765
A.txt 8642 722.01666
A.txt 8642 1819.0581
A.txt 8642 920.3196
A.txt 8642 1994.1241
~ and so on ~

==> B.txt <==

B.txt 11202 207.0211
B.txt 11202 1509.639
B.txt 11202 360.92783
B.txt 11202 1582.9225
B.txt 11202 497.07608
B.txt 11202 1668.4199
B.txt 11202 715.5241
B.txt 11202 1778.593
B.txt 11202 1006.15216
B.txt 11202 1933.0546
~ and so on ~

Upvotes: 1

Views: 121

Answers (1)

karakfa
karakfa

Reputation: 67467

you can fall back to awk default field splitting for exact matches instead of regex

$ awk '$1=="output"          {f=$2} 
       $1=="column" && $2==2 {n=$3} 
       $1+0==$1              {for(i=1; i<=NF; i++) print f,n,$i > f}' file

this will work regardless you have spaces or tabs. Last condition is to check whether the field is a number or not, again without regex.

Upvotes: 2

Related Questions