LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 09: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, 10:22 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Feed your find into a loop and echo name then cat file.
 
Old 04-28-2011, 01: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, 01:53 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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 09:21 AM.
 
Old 04-29-2011, 09: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, 09:49 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
The only bold part I see is colucix's name??
 
Old 04-29-2011, 10: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, 11:28 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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, 12:08 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 04:11 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 04:15 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 04:20 PM.
 
Old 04-29-2011, 04:35 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 04:44 PM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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



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 07:35 AM
Re: tars, how to use the extracted file? daffey1 Linux - Software 9 07-19-2009 02:48 AM
What happens if files are extracted and installed in wrong directory? gogoskiracer Linux - Newbie 4 02-03-2009 12:03 PM
Run CBT extracted from .rar files linuxqu Linux - Newbie 6 01-15-2008 04:31 AM
Best file location for include files Flip69 Linux - Newbie 2 06-13-2007 10:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:20 AM.

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
Open Source Consulting | Domain Registration