LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-10-2009, 05:04 PM   #1
pklcnu
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Rep: Reputation: 0
Thumbs up How to include the file name in the file


Hi

This is my first post please guide me if I am posting in the wrong forum.
I had few different files with the same prefix as the file name and my requirement is to combine all the files into one file with the file name displayed on the top of each files content, I can combine all the files into one file using 'cat' or 'sed' commands but how can I include the file name infront of each files content? Thanks very much in advance
 
Old 03-10-2009, 05:23 PM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,202

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
something like echo 'filename1' > outfile;cat file1 >> outfile; echo 'filename2' >> outfile; cat file2 >> outfile

> redirects and overwrites the contents of outfile
>> appends the contents of outfile
 
Old 03-11-2009, 05:05 AM   #3
pklcnu
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks Very Much

Quote:
Originally Posted by frieza View Post
something like echo 'filename1' > outfile;cat file1 >> outfile; echo 'filename2' >> outfile; cat file2 >> outfile

> redirects and overwrites the contents of outfile
>> appends the contents of outfile

Hi Frieza thanks very much for your reply and help, your time and help is much appreciated.

Last edited by pklcnu; 03-11-2009 at 05:18 AM. Reason: Not fully resolved the issue
 
Old 03-11-2009, 05:28 AM   #4
pklcnu
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Smile Hi Frieza my problem is not fully resolved

Quote:
Originally Posted by pklcnu View Post
Hi Frieza thanks very much for your reply and help, your time and help is much appreciated.
I tried the command that you sent it is working fine, but my requirement is like I had 100 files all with common prefix like 'Rej' and each file had different content in it. I need to combine all those files into one single file with the file name displayed on the top of its own content. I tried your command but everytime it is overwriting the one before. How can I see all in one along with file names ?

For example if I got two files with names Rej_abc and Rej_abcd. Suppose the content of Rej_abc is 'This is my first programme' and the content of second file Rej_abcd is 'This is my second programme'. Now I want a third out put file which includes both files content along with the names like the following.

'Rej_abc
This is my first programme
Rej_abcd
This is my second programme'
 
Old 03-11-2009, 05:44 AM   #5
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
If you're overwriting the file you must be using > instead of >> (see Frieza's post).

Try
Code:
for i in Rej*
  do echo $i >> outfile
     cat $i >> outfile
  done
Note: that will only work if there are no spaces in the filenames.

Last edited by openSauce; 03-11-2009 at 05:45 AM.
 
  


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
location of #include file? ragsnayak Linux - Software 2 11-17-2008 06:55 AM
No <db.h> include file found. natsume Linux - Software 1 04-17-2007 11:32 PM
C++ include file problem pwrlftr220 Programming 4 08-06-2006 11:26 PM
tar.gz file did not include install file |Spec-ops Linux - Newbie 12 11-17-2005 09:25 AM
#include file Cynthia Blue Linux - Newbie 6 03-31-2003 07:33 PM


All times are GMT -5. The time now is 03:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration