LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   include file name to extracted files (https://www.linuxquestions.org/questions/linux-newbie-8/include-file-name-to-extracted-files-877628/)

miss_dodi 04-28-2011 09:55 AM

include file name to extracted files
 
I've written the script below to merge only .txt files that exist in one directory into one huge .txt file and ignore other files with other extensions.
now the result is one huge .txt file with all the contents of other .txt files

how can i add the File Name as a comment before each file?


//FileName


Code:

system='/path/to/my/directory'
cat `find ${system}  -name '*.txt'` > outputFileDirectoryName.txt


grail 04-28-2011 10:22 AM

Feed your find into a loop and echo name then cat file.

David1357 04-28-2011 01:23 PM

Quote:

Originally Posted by miss_dodi (Post 4339457)
how can i add the File Name as a comment before each file?

Try
Code:

system='/path/to/my/directory'
output='outputFileDirectoryName.txt'
echo -n "" > $output
for file in $(find ${system} -name '*.txt') ; do
    echo "$file" >> $output
    cat $file >> $output
done


colucix 04-28-2011 01:53 PM

Code:

find ${system} -name '*.txt' -exec bash -c "echo -n \/\/ && basename '{}' | cat - '{}'" \; >> output.txt

MTK358 04-29-2011 09:18 AM

Code:

find "$path" -type f -name '*.txt' | while read file
do
    echo "// $file" >> "$outfile"
    cat "$file" >> "$outfile"
done

@colucix

I didn't think that find would replace instances of '{}' in a single argument string. Does that really work?

brownie_cookie 04-29-2011 09:43 AM

Quote:

Originally Posted by colucix (Post 4339701)
Code:

find ${system} -name '*.txt' -exec bash -c "echo -n \/\/ && basename '{}' | cat - '{}'" \; >> output.txt

OFFTOPIC: what does that bold part stand for? What is his purpose?

grail 04-29-2011 09:49 AM

The only bold part I see is colucix's name??

brownie_cookie 04-29-2011 10:01 AM

Quote:

Originally Posted by grail (Post 4340727)
The only bold part I see is colucix's name??

then you need to put your glasses on ;) (joking...)

its the part after the echo -n \/\/

that '\/\/' what does that do?

grail 04-29-2011 11:28 AM

Well actually I do wear glasses ... so check

The slashes are being escaped so as to not be used by the shell to mean a path to something, in this case root. Try this:
Code:

cd //

MTK358 04-29-2011 12:08 PM

Quote:

Originally Posted by grail (Post 4340851)
Well actually I do wear glasses ... so check

The slashes are being escaped so as to not be used by the shell to mean a path to something, in this case root. Try this:
Code:

cd //

How does that prove that "/" is special?

colucix 04-29-2011 04:11 PM

Quote:

Originally Posted by MTK358 (Post 4340687)
@colucix

I didn't think that find would replace instances of '{}' in a single argument string. Does that really work?

I'm not sure what do you mean as "single argument string". It is replaced by every item found and the command is executed multiple times over each argument. This is because of the trailing \; (escaped semicolon). On the other hand, if you use + (plus sign) the arguments are substituted all together and the command is executed only once:
Quote:

-exec command {} +
This variant of the -exec action runs the specified command on the selected files, but the command line is built
by appending each selected file name at the end; the total number of invocations of the command will be much less
than the number of matched files. The command line is built in much the same way that xargs builds its command
lines. Only one instance of `{}' is allowed within the command. The command is executed in the starting direc-
tory.

colucix 04-29-2011 04:15 PM

Quote:

Originally Posted by brownie_cookie (Post 4340722)
OFFTOPIC: what does that bold part stand for? What is his purpose?

It matches the requirements from the OP:
Quote:

how can i add the File Name as a comment before each file?


//FileName
I think s/he wanted to put some easily recognizable symbol in front of the file names in order to use the searching tools of whatever text editor s/he uses. And I agree with grail... the bold style inside code tags is almost indistinguishable from the rest... and my sight is eagle-like, despite my age! ;)

Seriously, the escaping in this case is an overkill, since the slashes are correctly passed to the invoked shell and they have no special meaning in this context. It was a sort of habit... maybe I was thinking about slash delimited regular expressions at that point! Indeed they are superfluous, here. :)

MTK358 04-29-2011 04:35 PM

Quote:

Originally Posted by colucix (Post 4341205)
I'm not sure what do you mean as "single argument string".

I mean that the argument contains {}, it's not "{}" alone.

colucix 04-29-2011 04:44 PM

Quote:

Originally Posted by MTK358 (Post 4341234)
I mean that the argument contains {}, it's not "{}" alone.

You can have multiple instances of {} in the exec command. The only caveat is that the argument {} is not correctly passed to command substitutions, that is the reference to the item is lost when spawning a subshell.


All times are GMT -5. The time now is 10:43 AM.