Merge multiple files found in config file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge multiple files found in config file
# 1  
Old 01-26-2012
Merge multiple files found in config file

I have a config file with a bunch of these type of blocks:

Code:
        <concat destfile="${standard.js.file}" append="true">
            <filelist dir="${js.dir}/foo" files="foo.js, foo2.js"/>
            <filelist dir="${js.dir}" files="foo3.js"/>
            <filelist dir="${js.dir}/bar/js" files="bar.js" />
            <!-- misc comment to throw things off a bit -->
            <filelist dir="${js.dir}" files="finalfoo.js"/>
        </concat>

I need to grab all these files, in the order they are here, and only within this section (the unique factor is the destfile value), and merge them all into a single file, newfoo.js

I already know how to take multiple files and concatenate them with cat file1 file2 > concatfile, but I'm struggling to figure out how to get the files out of the config file, then combine those.
# 2  
Old 01-26-2012
You mean you want only the data between the concat tags?

--ahamed
# 3  
Old 01-26-2012
I want the content of the files merged into one, so if the contents of ${js.dir}/foo/foo.js is 'foo' and ${js.dir}/foo/foo2.js contains 'bar', the first part of 'newfoo.js' would be:
Code:
foo
bar

and would be followed by the contents of all the other JS files within that <concat> element

---------- Post updated at 07:14 PM ---------- Previous update was at 06:42 PM ----------

To clarify:

I want to take that bit of code in the OP, come back with essentially this:

Code:
cat $jsdir/foo/foo.js $jsdir/foo/foo2.js $jsdir/foo3.js $jsdir/bar/bar.js $jsdir/finalfoo.js > /finalfoo.js

$jsdir will be defined in the calling script, so it'll look like this:

Code:
configfile="config.xml"
jsdir="./js/"

open $configfile
for every filelist between "${standard.js.file}" and "/concat"
files=/dir=\"${js.dir}(.+)\" files=\"(.+)\"/
for each file, prepend $jsdir/
sed $files s/\,// #(remove commas)
cat $files > /finalfoo.js

# 4  
Old 01-26-2012
Try this...
Code:
jsdir="./js/"

for file in $( awk -F"[=\"]" '/filelist/{gsub(/\./,"",$3); print $3}' infile )
do
        if [ -f "$file" ]
        then
                cat $file >> newfile.js
        fi
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 01-26-2012
Quote:
Originally Posted by ahamed101
Try this...
Code:
jsdir="./js/"

for file in $( awk -F"[=\"]" '/filelist/{gsub(/\./,"",$3); print $3}' infile )
do
        if [ -f "$file" ]
        then
                cat $file >> newfile.js
        fi
done

--ahamed
For testing, I've replaced the check to see if it's a file, and echo'd the file instead, so:

Code:
for file in $( awk -F"[=\"]" '/filelist/{gsub(/\./,"",$3); print $3}' infile )
do
    echo "$file"
done

And this is the output I get:
Quote:
${jsdir}/foo
${jsdir}
${jsdir}/prototip-2101/js #note: This should be /prototip-2.1.0.1 -- it apprears periods are being stripped out, but they shouldn't
${jsdir}
${cssdir} # from a separate <concat> -- needs to stay within the bounds of the standard.js.file concat
${jsdir}
${jsdir}
${jsdir}
${cssdir}
# 6  
Old 01-26-2012
Try this...
Code:
#!/bin/bash

jsdir="./js"

file_list=$(awk -F"[=\"]" '
        /standard.js.file/,/<\/concat>/{
        if(/filelist/){dir=$3;l=split($6,arr,", ")
        for(i=1;i<=l;i++){val=val" "dir"/"arr[i]}}}
        END{print val}' infile )

for file in $file_list
do
        eval echo ${file/./}
done

--ahamed

Last edited by ahamed101; 01-26-2012 at 11:08 PM.. Reason: Updated Code!
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 01-27-2012
Super close -- only problem is it's ouputting two forward slashes before the filename:

Code:
./path/to/scripts//foo.js


Last edited by Validatorian; 01-27-2012 at 01:02 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge Multiple html files into one

Hi all I have written some code to write my output in html. As i have multiple servers, need to generate single html file. but my code is generating html file for each server. I have merged the files using below code. cat /home/*_FinalData.html > /home/MergedFinalData.html But how to... (1 Reply)
Discussion started by: Snehasish
1 Replies

2. Shell Programming and Scripting

How to merge the multiple data files as a single file?

Hi Experts, I have created multiple scripts and send the output to new file, getting this output to my mailbox on daily basis. I would like to send the all outputs to a single file, need to merge all file outputs on a single file. For example, Created script for df -h > df.doc grep... (7 Replies)
Discussion started by: seenuvasan1985
7 Replies

3. Shell Programming and Scripting

Merge columns from multiple files

Hello and Good day I have a lot of files with same number of rows and columns.$2 and $3 are the same in all files . I need to merge $2,$3,$6 from first file and $6 from another files. File1: $1 $2 $3 $4 $5 $6... (8 Replies)
Discussion started by: ali.seifaddini
8 Replies

4. Shell Programming and Scripting

Merge the multiple text files into one file

Hi All, I am trying to merge all the text files into one file using below snippet cat /home/Temp/Test/Log/*.txt >> all.txt But it seems it is not working. I have multiple files like Output_ServerName1.txt, Output_ServreName2.txt I want to merge each file into one single file and... (6 Replies)
Discussion started by: sharsour
6 Replies

5. UNIX for Dummies Questions & Answers

Merge multiple files

Hi All, How can I merge 3rd column of multiple files into 1 file, the column header in the merged file being the name of the file from which the 3rd column was taken. The first 2 columns of all the files are exactly same. Thanks for your help ! (3 Replies)
Discussion started by: newbie83
3 Replies

6. UNIX for Advanced & Expert Users

merge two column multiple files into one

Hi I have multiple files each with two columns and I need to combine all those file into a tab delimited file. (multiple entry with same name separated by a comma) The content of the files are as follows: --- file1.txt: name var1 aaa xx aaa gg bbb yy ddd zz --- file2.txt ... (8 Replies)
Discussion started by: mary271
8 Replies

7. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

8. Shell Programming and Scripting

Merge Multiple Files and Transpose

Looking to join three files and then transpose some columns from multiple rows into a single row. File Info: FIELD TERMINATED BY '^' ENCLOSED BY '~' LINE TERMINATED BY '\r\n' FIRST FILE (FOOD_DES.txt) ~01001~^~0100~^~Butter, salted~^~BUTTER,WITH... (2 Replies)
Discussion started by: mkastin
2 Replies

9. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

10. UNIX for Advanced & Expert Users

Merge multiple .so files

Hi all, I am developing an application in Tcl, inwhich i have to load many modules written in C. I am converting those C modules into shared object(.so) files, and wrap it with my application using SWIG, for which i had the interface file. Now my question is, i have two different... (2 Replies)
Discussion started by: senthilvnr
2 Replies
Login or Register to Ask a Question