1

[![enter image description here][1]][1]I have the following set of files:

lh.aparc.DKTatlas40.stats
lh.BA.stats
lh.curv.stats
lh.w-g.pct.stats
rh.aparc.DKTatlas40.stats
rh.BA.stats
rh.curv.stats
rh.w-g.pct.stats
lh.aparc.a2009s.stats
lh.aparc.stats
lh.BA.thresh.stats
lh.entorhinal_exvivo.stats
rh.aparc.a2009s.stats
rh.aparc.stats
rh.BA.thresh.stats
rh.entorhinal_exvivo.stats

I want to combine each 'rh' file with each 'lh' file For example I need to combine lh.aparc.DKTatlas40.stats + rh.aparc.DKTatlas40.stats = merge.aparc.DKTatlas40.stats

How do I do it?

2
  • There is a 'rh' for each 'lh' They are basically values for left side and right side of the brain....... so the measures inside the files are also same ......just that they are separated in left and right ...... Commented Feb 23, 2017 at 18:14
  • 3
    You can't accept an answer and then change the question... Well you can, but it invalidates the given answers.
    – Kusalananda
    Commented Feb 23, 2017 at 18:39

2 Answers 2

2
for lh in lh*; do
    cat "$lh" "rh${lh#lh}" >"merge${lh#lh}"
done

This will concatenate each lh file with the corresponding rh file and save the concatenated version as merge (followed by whatever comes after lh in the filename).

The parameter expansion ${lh#lh} will remove the string lh from the start of the value of $lh (e.g. turn lh.BA.stats into .BA.stats).

2
  • Thanks I did! My issue now is that the columns are repeating?How do I solve this? Commented Feb 23, 2017 at 18:33
  • @ShreyaChakrabarti Do you mean that you have duplicates in the contents of the concatenated files? We don't know anything about the contents of those files. You'd better submit a new question about that.
    – Kusalananda
    Commented Feb 23, 2017 at 18:35
0

Since your files are paired, you can loop through one and intuit the name of the other:

for f in lh.*; do
    cat "$f" "${f/lh/rh}" > "${f/lh/merge}"
done

The above loop will create a merge.*.stats file for each lh/rh pair.

The construct ${var/foo/bar} will replace the first instance of foo in the variable var with bar; we do this first to intuit the name of the paired file, and then to define the filename for the merged result of the concatenation.

You must log in to answer this question.

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