0

I need a shell script which takes in the path of directories as arguments. The directory contains similar named files for eg. file1.dat, file2.dat etc. I need to take file1.dat from all the directories - remove the headers - extract the second column from all and save it as a different file with horizontal concatenation.

Something like this:

$ combine.sh dir1/ dir2/... dirn/

My number of directories are not fixed - it can be in the range of 1-6

dir1/file1.dat

#header
1 5 8 
2 6 9 
3 7 10

dir2/file1.dat

#header
55 15 80
65 25 81
75 35 82

I need an output like

final_file1.dat

5 15
6 25
7 35

My script looks like :

@@combine.sh

#!/bin/sh

for i in "$@"
do
tail -n +1 $i/file1.dat | awk "{print$2}' >> temp_file1.dat
paste -d ' ' final_file1.dat temp_file1.dat >> final_file1.dat
done

When i execute this script it comes as

5
6
7
8
1
2
3
4

I am not sure how to proceed. Any help would be appreciated.

Your Answer

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

Browse other questions tagged or ask your own question.