Shell Script to append files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Shell Script to append files

Shell Programming and Scripting


Tags
append, copy, script, shell

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-11-2010
rac.mike rac.mike is offline
Registered User
 
Join Date: Sep 2010
Last Activity: 11 September 2010, 11:12 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Shell Script to append files

I have several content (text) files in a folder called "content"

I have several google ads (text) files in a folder called "google_ads"

Example:


Code:
/content
    /java
       websphere.txt
       android.txt
   /microsoft
      framework.txt
      /c_sharp
         linq.txt


Code:
/google_ads
    /java
       websphere.txt
       android.txt
   /microsoft
      framework.txt
      /c_sharp
         linq.txt

I want to create a shell script that appends the files together and produces them in a "publish" folder


Code:
/publish
     /java
        websphere.txt
        android.txt
    /microsoft
       framework.txt
       /c_sharp
          linq.txt

How do I go about doing this?
Sponsored Links
    #2  
Old 09-12-2010
Aia's Avatar
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 16 November 2014, 2:02 PM EST
Posts: 571
Thanks: 32
Thanked 175 Times in 166 Posts
cat is your friend there


Code:
cat file_one file_two file_three > publish_folder/big_mother_file

The Following User Says Thank You to Aia For This Useful Post:
lcoreyl (10-25-2012)
Sponsored Links
    #3  
Old 09-12-2010
rac.mike rac.mike is offline
Registered User
 
Join Date: Sep 2010
Last Activity: 11 September 2010, 11:12 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Aia View Post
cat is your friend there


Code:
cat file_one file_two file_three > publish_folder/big_mother_file

yes. but I have 100s / 1000s of files (in several sub directories).

How do I do this in a recursive fashion?
    #4  
Old 09-12-2010
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
 
Join Date: Dec 2007
Last Activity: 21 August 2014, 4:30 PM EDT
Posts: 1,136
Thanks: 2
Thanked 88 Times in 85 Posts
yes


Code:
cat content/*.txt google_ads/*.txt foobar/*.txt >publish.file


if you hit a maximum arguments error try this.


Code:
find . -name \*.txt -exec cat {} \; > publish.file

Sponsored Links
    #5  
Old 09-12-2010
konsolebox konsolebox is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 30 August 2014, 6:29 AM EDT
Posts: 48
Thanks: 0
Thanked 10 Times in 9 Posts
Try to run this on a script. You should be on the parent directory of content and google_ads

Code:
#!/bin/bash

function main {
	while read LINE; do
		LINE=${LINE#*/}
		echo "creating directory publish/$LINE..." || {
			echo "failed."
			return 1
		}
		mkdir -p "publish/$LINE"
	done < <(exec find content/ -type d)

	while read LINE; do
		LINE=${LINE#*/}
		echo "making file publish/$LINE..."
		{
			cat "content/$LINE"
			[[ ! -f google_ads/$LINE ]] || cat "google_ads/$LINE"
		} > "publish/$LINE"
		[[ $? -eq 0 ]] || {
			echo "failed."
			return 1
		}
	done < <(exec find content/ -type f -iname '*.txt')
}

main

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To Create shell script files from a shell script NehaB Shell Programming and Scripting 3 07-28-2010 12:29 AM
Shell script to identify the number of files and to append data pradkumar Shell Programming and Scripting 4 05-26-2010 05:52 AM
How to append value at first line of CSV file using shell script? anujrichhariya Shell Programming and Scripting 7 11-26-2009 11:28 AM
shell script to read a line in gps receiver log file and append that line to new file gudivada213 Shell Programming and Scripting 3 06-24-2009 07:16 AM
Shell script to append a time for the existing error log file gsprasanna UNIX for Advanced & Expert Users 12 07-12-2007 06:07 AM



All times are GMT -4. The time now is 08:05 AM.