0
NAME    10/01   09/02   09/07   09/08   09/09   09/10   09/11   09/12   09/13   09/14   09/15   09/16   09/17   09/18   09/19   10/01
CNMPMT_DATA 99.22       99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22
DATA01  9.79    10.75   13.67   7.97    15.22   15.1    14.63   15.71   15.56   15.37   16.69   17.33   17.06   16.5    18.01   99.22
DATATBLS4PEAS   9.21    7.45    51.77   8.26    54.56   54.43   50.68   42.2    38.23   53.47   51.02   49.53   48.01   46.58   46.34   99.22
EDD_DATA        57.79   57.71   58.46   57.58   57.93   59.09   58.88   58.88   58.83   58.79   58.77   58.73   58.7    59.69   59.34   99.22
EDD_INDX        23.34   23.44   22.96   23.27   23.07   22.94   22.79   22.94   23.06   23.03   23.13   23.25   23.08   22.81   23.05   99.22
INDX01  8.59    8.74    8.68    8.72    8.94    8.71    8.46    8.78    8.87    8.82    9.19    9.41    9.21    8.86    9.38    99.22
SYSAUX  96.65   96.67   96.62   96.92   96.55   96.57   96.56   96.59   96.57   96.63   96.65   96.7    96.72   96.73   96.65   99.22
SYSTEM  93.37   93.37   93.36   93.38   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   99.22
TEMP    97.43   98.50   98.54   88.77   99.54   95.68   99.54   99.52   99.25   99.35   96.44   98.87   98.53   98.54   98.13   99.22
TEMP_EDDDATA    96.58   96.58   96.58   96.58   96.58   96.58   96.58   96.55   96.55   96.55   96.55   96.54   96.37   96.37   96.38   99.22
UNDOTBS1        22.35   14.31   34.18   30.17   5.37    8.94    17.08   17.43   14.7    22.96   11.99   7.83    .02     8.07    3.67    99.22
UNDOTBS2        42.45   47.22   53.84   43.96   25.77   36.5    40.05   28.89   27.75   26.86   23.4    22.86   .02     .03     6.56    99.22
UNDOTBS3        68.65   72.3    39.89   28.95   44.52   69.05   71.89   52.23   48.23   41.67   31.52   30.09   16.98   23.44   18.54   99.22
USERS   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   99.22

This file I want only column with September month. file is Tab delimited. Tried below its working but it changes the column position

awk 'NR==1{for(i=1; i<=NF; i++) if ($i ~"09") {a[i]++;} } { for (i in a) printf "%s\t", $i; printf "\n"}' Allen_Free.txt


09/07   09/08   09/09   09/10   09/11   09/12   09/13   09/14   09/15   09/16   09/17   09/18   09/19   09/02
99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22
13.67   7.97    15.22   15.1    14.63   15.71   15.56   15.37   16.69   17.33   17.06   16.5    18.01   10.75
51.77   8.26    54.56   54.43   50.68   42.2    38.23   53.47   51.02   49.53   48.01   46.58   46.34   7.45
58.46   57.58   57.93   59.09   58.88   58.88   58.83   58.79   58.77   58.73   58.7    59.69   59.34   57.71
22.96   23.27   23.07   22.94   22.79   22.94   23.06   23.03   23.13   23.25   23.08   22.81   23.05   23.44
8.68    8.72    8.94    8.71    8.46    8.78    8.87    8.82    9.19    9.41    9.21    8.86    9.38    8.74
96.62   96.92   96.55   96.57   96.56   96.59   96.57   96.63   96.65   96.7    96.72   96.73   96.65   96.67
93.36   93.38   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.37
98.54   88.77   99.54   95.68   99.54   99.52   99.25   99.35   96.44   98.87   98.53   98.54   98.13   98.50
96.58   96.58   96.58   96.58   96.58   96.55   96.55   96.55   96.55   96.54   96.37   96.37   96.38   96.58
34.18   30.17   5.37    8.94    17.08   17.43   14.7    22.96   11.99   7.83    .02     8.07    3.67    14.31
53.84   43.96   25.77   36.5    40.05   28.89   27.75   26.86   23.4    22.86   .02     .03     6.56    47.22
39.89   28.95   44.52   69.05   71.89   52.23   48.23   41.67   31.52   30.09   16.98   23.44   18.54   72.3
22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09

09/02 should come first and then 09/07 not sure what is the issue.

Please help.

Neha
  • 67
  • 8

2 Answers2

1

If perl solution is okay,

$ perl -MList::MoreUtils=indexes -nale '@i = indexes { /^09/ } @F if $. == 1; print join "\t", @F[@i]' ip.txt 
09/02   09/07   09/08   09/09   09/10   09/11   09/12   09/13   09/14   09/15   09/16   09/17   09/18   09/19
99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22   99.22
10.75   13.67   7.97    15.22   15.1    14.63   15.71   15.56   15.37   16.69   17.33   17.06   16.5    18.01
7.45    51.77   8.26    54.56   54.43   50.68   42.2    38.23   53.47   51.02   49.53   48.01   46.58   46.34
57.71   58.46   57.58   57.93   59.09   58.88   58.88   58.83   58.79   58.77   58.73   58.7    59.69   59.34
23.44   22.96   23.27   23.07   22.94   22.79   22.94   23.06   23.03   23.13   23.25   23.08   22.81   23.05
8.74    8.68    8.72    8.94    8.71    8.46    8.78    8.87    8.82    9.19    9.41    9.21    8.86    9.38
96.67   96.62   96.92   96.55   96.57   96.56   96.59   96.57   96.63   96.65   96.7    96.72   96.73   96.65
93.37   93.36   93.38   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36   93.36
98.50   98.54   88.77   99.54   95.68   99.54   99.52   99.25   99.35   96.44   98.87   98.53   98.54   98.13
96.58   96.58   96.58   96.58   96.58   96.58   96.55   96.55   96.55   96.55   96.54   96.37   96.37   96.38
14.31   34.18   30.17   5.37    8.94    17.08   17.43   14.7    22.96   11.99   7.83    .02 8.07    3.67
47.22   53.84   43.96   25.77   36.5    40.05   28.89   27.75   26.86   23.4    22.86   .02 .03 6.56
72.3    39.89   28.95   44.52   69.05   71.89   52.23   48.23   41.67   31.52   30.09   16.98   23.44   18.54
22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09   22.09
  • -MList::MoreUtils=indexes use indexes function from List::MoreUtils module
  • -ale split input line on spaces and save to @F array, newline in input line is stripped and added while printing
  • @i = indexes { /^09/ } @F if $. == 1 if first line, get index of all elements in @F array matching the regex /^09/
  • print join "\t", @F[@i] print the elements of @F matching index saved in @i array, separated by tabs

If List::MoreUtils module is not available,

perl -nale '@i = grep { $F[$_] =~ /^09/ } 0..$#F if $. == 1; print join "\t", @F[@i]' ip.txt
Sundeep
  • 23,246
  • 2
  • 28
  • 103
  • Thank you Sundeep. getting this Can't locate List/MoreUtils.pm in @INC (@INC contains: and I am not familiar with perl – Neha Sep 21 '16 at 13:54
  • `cat text1.txt | perl -F'\t' -nae ' my ($s,$m,$h,$md,$month) = localtime(); $mon=sprintf("%2.2d", $month+1); print $mon chomp @F; if (/^NAME/) { @columns = @F; } for $i (0..$#F) { if ($i == 0 || $columns[$i] =~ /^$mon/) { print "\t" if $i != 0; print $F[$i]; }; } print "\n" '` this worked – Neha Sep 21 '16 at 16:51
0

awk is really useful for processing data by rows, so you could tranpose your data first.

This is how I would do it:

From An efficient way to transpose a file in Bash

function transpose() {
    awk '{ 
        for (i=1; i<=NF; i++)  {
            a[NR,i] = $i
        }
    }
    NF>p { p = NF }
    END {    
        for(j=1; j<=p; j++) {
            str=a[1,j]
            for(i=2; i<=NR; i++){
                str=str" "a[i,j];
            }
            print str
        }
    }'
}

Then

cat file.txt | transpose | awk 'NR==1 { print $0 } /[0-9]+\/09/ { print $0 }' | transpose
Community
  • 1
  • 1
  • Thank you for your response but function is giving error – Neha Sep 21 '16 at 02:28
  • Removed () after transpose it worked however its showing first column that is name and 09/09 column `NAME 09/09 CNMPMT_DATA 99.22 DATA01 15.22 DATATBLS4PEAS 54.56 EDD_DATA 57.93 EDD_INDX 23.07 INDX01 8.94 SYSAUX 96.55 SYSTEM 93.36 TEMP 99.54 TEMP_EDDDATA 96.58 UNDOTBS1 5.37 UNDOTBS2 25.77 UNDOTBS3 44.52 USERS 22.09` – Neha Sep 21 '16 at 02:33
  • That's probably because you are not typing these commands into a bash shell. You don't actually have to definte a function, but it's nicer since it's reused. You can drop `NR==1 { print $0 }` and it won't print the row names. – Kurt McAlpine Sep 21 '16 at 02:34
  • Still giving value only for row 09/09 – Neha Sep 21 '16 at 02:40
  • Change the regex to match the patten you want. – Kurt McAlpine Sep 21 '16 at 03:04