rkmca
1
I'm trying to combine colums from multiple file to a single file but having some issues, appreciate your help.
The filenames are the same except for the extension,
path1.m0
---------
a b c
d e f
g h i
path1.m1
---------
m n o
p q r
s t u
File names are path1.m[0 - 1]
The desired output file is path1.m and output is
a n
d q
g t
I can do this by the paste command
paste path1.m0 path1.m1 | awk '{print $1 $5}' > path1.m
The issue I'm having is that I've multiple files like
path2.m0
path2.m1
path3.m0
path4.m1
How do I set a variable to read all the path1.* and path2.* and path3* files and give the output files
path1.m
path2.m
path3.m
Thanks very much.
agama
2
This would be one way:
#!/usr/bin/env ksh
for f0 in path*.m0
do
f1=${f0%.*}.m1 # create the second filename
paste $f0 $f1 | awk '{print $1, $5}' >${f0%.*}.m
done
It assumes that all of your files start with the same string "path".
You can make the code a bit more terse by not assigning f1, and just creating the name 'in line'
paste $f0 ${f0%.*}.m1 | awk '{print $1 $5}' > ${f0%.*}.m
Probably works in bash, but I didn't test it there.
rkmca
3
Thanks. Not working, I think its shell issue. Using tcsh and command not found.
agama
4
Yep, it's Korn shell. You could put it into a file and execute it. Make sure that the leading #! line is kept.
It's been too long since I've written any csh code, so I've got no suggestions along those lines.