0

I have ~2000 .txt files that each contain three columns similar to this example, where columns 1-2 are the same in each file:

A   Func1   6
B   Func1   5
C   Func1   17
D   Func1   47
E   Func1   37
F   Func1   42
G   Func1   22
H   Func1   13
I   Func1   81

I want to collate all data from each .txt file into one results file. I just need the data from column 3 oriented into rows for each file. I also need the file name in the first column:

file_name   A  B  C  D  E  F  G  H  I
file1.txt   6  5  17 47 37 42 22 13 81
file2.txt   8  12 22 84 1  43 54 90 10
...

Thanks!

3

Using a shell loop with awk and paste:

for f in *.txt
do
    printf "%s\t" "$f"   # print filename
    awk '{print $3}' "$f" | paste -s  # take third column and transpose
done

With output like so:

file1.txt   6   5   17  47  37  42  22  13  81
file2.txt   12  11  77  107 97  102 82  73  141

The header line can be generated from any file:

printf "%s\t" "file_name"
awk '{print $1}' file1.txt | paste -s
  • Thanks @muru ! I just realised that the 2nd column has (a variable number of) spaces in it so I actually want to take the last column instead of the 3rd column from each file... how would I do that? Thanks! – user2861089 Jul 17 '18 at 5:23
  • 1
    @user2861089 in the awk command, use $NF instead of $3. – muru Jul 17 '18 at 5:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.