1

In a parent folder I have multiple folders inside it. Within each folder I have text file "text.txt". The text files are similar in all the folders, each text file contain 100 line and one column of numbers. example

cat /folder1/text1.txt

1654
1684
535
35131
.
.

I want to merge all these text files as columns in one file using the command paste. In the parent folder I ran the command

paste ./*/text*.text > all_text.txt  # the content for all_text.txt is as follow:

cat all_text.txt
1654        354531 ....
1684        224
535         2424 
35131       24
.
.
.

How can I add the folders names as a header for each pasted column to get the following output

cat all_text.txt
folder#1   folder #2   .....
1654        354531
1684        224
535         2424 
35131       24
 .
 .
 .   
0

You could use set and parameter expansion on each array element to print just the directory name:

set -- */text.txt
{ printf '      %s' "${@%/*}" | cut -c2-;  paste -- "$@"; }
# this blank ^ is a literal tab
  • Thank you very much ! Your help is highly appreciated :) – user88036 Nov 11 '16 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy