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-2008, 09:24 AM   #1
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Rep: Reputation: 30
Append files


When I usually want to create a file made up of many files, I usually just use the cat command and just append the file to an existing file:

cat file1.txt >> bunch_of_files.txt

However, what would be the best way of doing this but have the title of the file proceed the file itself?..meaning if file1.txt was appended to bunch_of_files.txt, I would like "file1.txt" to start on a newline before the actual contents of file1.txt.
 
Old 03-10-2008, 09:57 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,478

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
At it's simplest,
Code:
cat file >> bunch_of_files.txt
echo "" >> bunch_of_files.txt
cat file1.txt >> bunch_of_files.txt
 
Old 03-10-2008, 11:20 AM   #3
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Original Poster
Rep: Reputation: 30
Actually, what I mean is that I would want the file name to precede the contents of the file. This is what bunches_of_files.txt would look like:


file1.txt

"Contents of file1.txt"
fdsaf
fdsafd
fdas
fdas
fdsa
fdsa


file2.txt
"Contents of file2.txt"
fdas
fdsa
fdsa
fdas
fdsa
fdsafdsa


etc....


Obviously I could do this manually, but I was wondering if there is a way to both append a file and include the title of the file with one command.

Last edited by tekmann33; 03-10-2008 at 11:25 AM.
 
Old 03-10-2008, 11:34 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
touch bunch_of_files.txt
for file in file*.txt; do 
   echo $file >> bunch_of_files.txt
   echo "" >> bunch_of_files.txt
   cat $file >> bunch_of_files.txt
done
edit: It assumes bunch_of_files.txt doesn't exist.

Last edited by pwc101; 03-10-2008 at 11:37 AM.
 
Old 03-10-2008, 11:35 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,478

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Well, if I was going to do this I would
Code:
#!/bin/bash (or whatever Bourne-like shell you use)
for file in *.txt
do
     echo ${file} >> output
     cat ${file} >> ouput
     echo "" >> ouput
done
and save that as a shell program.

Possible changes would be to accept a command-line argument and process that or those...
 
Old 03-10-2008, 11:40 AM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
Quote:
Originally Posted by tekmann33 View Post
Obviously I could do this manually, but I was wondering if there is a way to both append a file and include the title of the file with one command.
Not really, but you can try two commands together:
Code:
echo -e "file1\n" | cat - file1 > bunches_of_files.txt
 
Old 03-10-2008, 11:41 AM   #7
sandgroper
Member
 
Registered: Jul 2004
Location: Perth , Western Australia
Distribution: Fedora Core 5 , Mint 9
Posts: 118

Rep: Reputation: 15
Smile

Quote:
Originally Posted by tekmann33 View Post
Actually, what I mean is that I would want the file name to precede the contents of the file. This is what bunches_of_files.txt would look like:


file1.txt

"Contents of file1.txt"
fdsaf
f
file2.txt
"Contents of file2.txt"
fdas
fdsa
f

etc....


Obviously I could do this manually, but I was wondering if there is a way to both append a file and include the title of the file with one command.
You could write a small script that takes 2 commandline parameters for the 2 files .
The added fact is that is will be a generic script so that you can use any 2 files.



Code:
#!/bin/sh
# script name : addfiletext

if ( test $# != 0 )
  then
      echo " " >> $2  # add empty line to file2
      echo $1 >> $2   # add file1 name to file
      cat $1 >> $2    # add contents of file1 to file2
else
    echo addfiletext: 'requires two arguments'
fi
Off course this is just a quick and basic script , without much error checking or refinement , but you should be able to get an idea.
 
  


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
[C++] append to each row of a text files sylvaticus Programming 2 10-18-2007 08:22 AM
how to append files using cpio? madhugp Linux - General 8 01-04-2007 09:53 AM
append files Paul_R Linux - General 6 11-04-2005 07:15 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 10:28 AM
Trying to Append files to an already made Tar bluedevlx Linux - Newbie 10 11-04-2004 08:37 AM


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