2

I want to extract particular column from the file1 by comparing with file2 in which column number is given that should be extracted from file1.

File 1 (source data) look like this:

  1  2  3  4  5 10 11 14  
  13 25 37 2 4 7  9 23  
  12  12 23 15 17 18 24 25

File 2 (with the column numbers to extract):

  2  
  4  
  5

So I want to compare both file1 and file2. Using file 2 I want to extract columne number 2,4,5 from file1.

Desired output:

  2 4 5  
  25 2 4  
  12 15 17  

How can I proceed for that?

2 Answers 2

3

Try also

awk '
FNR == NR       {COL[NR] = $1                   # get column numbers from file2
                 MX = NR                        # retain max line No. in file2
                 next
                }
                {for (i=1; i<=MX; i++)  printf "%s%s", $(COL[i]), (i==MX)?ORS:OFS
                                                # print those columns, and field
                                                # or line separator
                }
' file2 file1
2 4 5
25 2 4
12 15 17
5
  • This is exactly the right way to do it.
    – Ed Morton
    Commented Jan 22, 2020 at 14:44
  • @RudiC same problem is coming in your script. It is working if file 2 has some number to compare. But If file 2 is blank then it printing full file 1 as out put file. While it should write zero. Commented Jan 22, 2020 at 16:18
  • Just change COL[NR] = $1; MX = NR to if (/[0-9]/) COL[++MX] = $1. That'll skip any lines in file2 that don't contain a field number.
    – Ed Morton
    Commented Jan 22, 2020 at 17:54
  • it is also not working, giving a same result Commented Jan 23, 2020 at 4:07
  • Difficult to believe. With Ed Morton's adaption, empty or non-numeric lines - which would result in a $0 (complete line) printout - are rejected.
    – RudiC
    Commented Jan 23, 2020 at 20:47
1

With Perl:

perl -pale '$"="\t";
   chomp(@A = map { $_-1 } grep { /^[1-9]\d*$/m } <STDIN>) if $. == 1;
   $_ = @A ? "@F[@A]" : last;
' File1 < File2

Result:

2   4   5
25  2   4
12  15  17

Explanation:

Give the column numbers file (one column num per line) on the stdin to the perl utility and the data file on Perl's commandline.

Set the array element concatenator ($") to a TAB so that all output fields are TAB-separated.

Quit the program as soon as we detect that the array of columns to be printed, @A, is found to be empty. It only comprises those lines from File2 that have one positive integer per line. any other combination is rejected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .