I have a folder which has multiple sub-folders and sub sub folders. I want to print the content of a file called result.txt
which is present in many sub folders or sub sub folders into a csv file along with the name of the sub-folder.
That means if the files named result.txt
are in
abc/def/result.txt
efg/result.txt
Then I need a csv file which should have
1. abc content of its result.txt
2. efg content of its result.txt
and so on.
I started with the following find
command
find . -iname 'result.txt' "a portion of path" "content">final.csv
How should I proceed from here ?
Note: (8th December, 2017) Although the below solutions display the content properly on the terminal , none of them work when I add >final.csv. As already mentioned, my result.txt has mutilines. The content of a particular result.txt gets spilled to different cells rather than being in a single cell. Any suggestions ?
Best Answer
I think
find
is the right choice:Example run
Explanations
This
find
command searches every file in or under the current directory of the nameresult.txt
andexec
utes theprintf
command in abash
subshell. Theprintf
command prints the subdir's name, a comma and the file content followed by a\n
ewline. If you want to write this output to a file, just append e.g.>final.csv
to the command.Even simpler
is the
-printf
approach suggested by steeldriver:This prints an additional slash in the first column which you can easily remove by piping the output through e.g.
sed 's|/,|,|'
.Merging multiline
result.txt
content into one cellTo replace newline characters with e.g. spaces just replace
cat
withsed ":a;N;\$!ba;s/\n/ /g"
in one of the above commands, e.g.If you want some other string as the delimiter replace the
/ /
part with/your_delimiter/
, but keep the slashes.