joining multiple files into one while putting the filename in the file

Hello, I know how to join multiple files using the cat function. I want to do something a little more advanced. Basically I want to put the filename in the first column...

One thing to note is that the file is tab delimited.

e.g.

file1.txt

joe 1 4 5 6 7 3
manny 2 3 4 5 6 7
file2.txt

bob 1 5 6 7 4 3
larry 2 4 3 2 1 2

so i want to join both files so it looks like this

file1 joe 1 4 5 6 7 3
file 1 manny 2 3 4 5 6 7
file 2 bob 1 5 6 7 4 3
file 2 larry 2 4 3 2 1 2

:confused:

thanks

Replace "<tab>" with a literal tab character in the following - or adjust the output to your needs altogether. The idea should be clear enough.

#!/bin/ksh

set -A afFile "file1" "file2" "file3"
typeset -i iFileCnt=0

while [ $iFileCnt -lt ${#afFile[*]} ] ; do
     sed 's/^/'"${afFile[$iFileCnt]}"'<tab>/' "${afFile[$iFileCnt]}"
     (( afFile += 1 ))
done

exit 0

I hope this helps.

bakunin

hi it only prints out teh first file but not the next ones

$ awk '{print FILENAME,$0}' file1 file2

thats perfect!

thanks