LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 04-28-2011, 10:55 AM   #1
miss_dodi
LQ Newbie
 
Registered: Apr 2011
Posts: 1

Rep: Reputation: 0
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
 
Old 04-28-2011, 11:22 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Feed your find into a loop and echo name then cat file.
 
Old 04-28-2011, 02:23 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by miss_dodi View Post
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
 
Old 04-28-2011, 02:53 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
find ${system} -name '*.txt' -exec bash -c "echo -n \/\/ && basename '{}' | cat - '{}'" \; >> output.txt
 
Old 04-29-2011, 10:18 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
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?

Last edited by MTK358; 04-29-2011 at 10:21 AM.
 
Old 04-29-2011, 10:43 AM   #6
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
Quote:
Originally Posted by colucix View Post
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?
 
Old 04-29-2011, 10:49 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
The only bold part I see is colucix's name??
 
Old 04-29-2011, 11:01 AM   #8
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
Quote:
Originally Posted by grail View Post
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?
 
Old 04-29-2011, 12:28 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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 //
 
Old 04-29-2011, 01:08 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by grail View Post
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?
 
Old 04-29-2011, 05:11 PM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by MTK358 View Post
@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.
 
Old 04-29-2011, 05:15 PM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by brownie_cookie View Post
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.

Last edited by colucix; 04-29-2011 at 05:20 PM.
 
Old 04-29-2011, 05:35 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by colucix View Post
I'm not sure what do you mean as "single argument string".
I mean that the argument contains {}, it's not "{}" alone.
 
Old 04-29-2011, 05:44 PM   #14
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by MTK358 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding files extracted from an ISO to GRUB2 bootloader OverKll Linux - Software 3 02-22-2010 08:35 AM
Re: tars, how to use the extracted file? daffey1 Linux - Software 9 07-19-2009 03:48 AM
What happens if files are extracted and installed in wrong directory? gogoskiracer Linux - Newbie 4 02-03-2009 01:03 PM
Run CBT extracted from .rar files linuxqu Linux - Newbie 6 01-15-2008 05:31 AM
Best file location for include files Flip69 Linux - Newbie 2 06-13-2007 11:26 AM


All times are GMT -5. The time now is 06:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration