Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the folder name as a column in a text file

The lazy me is thinking about adding a column to some textfiles.

The textfiles are in directories and I would like to add the directory name to the text file.

Like the text file text.txt in the folder the_peasant:

has a wart    
was dressed up like a witch     
has a false nose

would become:

the_peasant has a wart    
the_peasant was dressed up like a witch    
the_peasant has a false nose

Then I have similar text files in other folders called "the_king" etc.

I would think this is a combination of the find command, bash scripting and sed but I cant see it through. Any ideas?

like image 688
AWE Avatar asked Feb 22 '12 22:02

AWE


1 Answers

This might work for you:

find . -name text.txt | sed 's|.*/\(.*\)/.*|sed -i "s@^@\1 @" & |' | sh

or if you have GNU sed:

find . -name text.txt | sed 's|.*/\(.*\)/.*|sed -i "s@^@\1 @" & |e' 
like image 193
potong Avatar answered Sep 20 '22 16:09

potong