Joe Joe - 17 days ago 5
Linux Question

Merge Files and Prepend Filename and Directory

I need to merge files in a directory and include the directory, filename, and line number in each line of the output. I've found many helpful posts about including the filename and line number but not the directory name. Grep -n gets line numbers and I've seen some find commands that get some of the other parts but I can't seem to pull them all together. (I'm using Ubuntu for all of the data processing.)

Imagine two files in directory named "8". (Each directory in the data I have is a number. The data were provided that way.)

file1.txt


John
Paul
George
Ringo


file2.txt


Mick
Keef
Bill
Brian
Charlie


The output should look like this:


8:file1.txt:1:John
8:file1.txt:2:Paul
8:file1.txt:3:George
8:file1.txt:4:Ringo
8:file2.txt:1:Mick
8:file2.txt:2:Keef
8:file2.txt:3:Bill
8:file2.txt:4:Brian
8:file2.txt:5:Charlie


The separators don't have to be colons. Tabs would work just fine.

Thanks much!

Answer

If it's just one directory level deep you could try something like so. We go into each directory, print each line with its number and then append the directory name to the front with sed:

$ for x in `ls`; do
   (cd $x  ; grep -n . *) | sed -e 's/^/'$x:'/g'
 done

1:c.txt:2:B
1:c.txt:3:C
2:a.txt:1:A
2:a.txt:2:B
Comments