Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating multiple files based on contents from 1 file. 1

Status
Not open for further replies.

GilmarACN

Programmer
Jan 10, 2014
3
0
0
Hello, I am new to AWK and I was wondering if I can use AWK for this:

I have to generate multiple files for a performance testing task where only the filename and a string within the file would be incremented for each test file. We need to generate thousands of files which is why we are looking for an automated solution. Can I create a "master" file for this and have AWK use this file to generate all my test files? Here's what I'm thinking:

Master File contains:
1. filename = WAWF_I_002-TEST-XXXX.xml (where XXXX will be incremented for each file)
2. include a variable within the data to increment my invoice number to correspond to the filename (<Invoice_Document.Invoice_Identification.Identifier>6U02130228-XXXX</Invoice_Document.Invoice_Identification.Identifier>)
3. all remaining data to be included on each file as noted except for the invoice number (possibly surrounded by {} or any other special character to indicate the beginning and ending of the data block)

Any thoughts on this? Will this be a good approach to take? Your help with this will be greatly appreciated.
Thanks,
Gilmar
 

Try something like this:
Code:
==> cat m4
# This is the master xml file, notice the text to replace is '%INVOICE%'
#
cat - <<! >master.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<document><controlData><systemName>ADMIN</systemName>
<generationUserId>ElkaBrwn01</generationUserId>
<generationDate>03172011</generationDate>
<generationTime>193800</generationTime>
<generationProcedure/>
<formType>INVOICE</formType>
<documentClass>CI</documentClass>
<Invoice_Document.Invoice_Identification.Identifier>%INVOICE%</Invoice_Document.Invoice_Identification.Identifier>
<Amount>153775.12</Amount>
<actionRequested>create</actionRequested>
!
rm WAWF_I_002-TEST-*.xml 2>&1
>MasterFile
max=3                    #<== number of files to generate
i=0
while [ i -le max ]
do
  (( i+=1 ))
  SEQNO=`printf "%04d" $i`
  OUTFILE="WAWF_I_002-TEST-${SEQNO}.xml"
  INVOICE="6U02130228-${SEQNO}"
  echo "$i - $OUTFILE $INVOICE"
  sed "s/%INVOICE%/$INVOICE/" master.xml > $OUTFILE
  echo "$OUTFILE $INVOICE" >>MasterFile
done

==> ./m4
1 - WAWF_I_002-TEST-0001.xml 6U02130228-0001
2 - WAWF_I_002-TEST-0002.xml 6U02130228-0002
3 - WAWF_I_002-TEST-0003.xml 6U02130228-0003
4 - WAWF_I_002-TEST-0004.xml 6U02130228-0004

==> l|grep WAWF
-rw-r--r--    1 oracle   dba             504 Jan 10 14:28 WAWF_I_002-TEST-0004.xml
-rw-r--r--    1 oracle   dba             504 Jan 10 14:28 WAWF_I_002-TEST-0003.xml
-rw-r--r--    1 oracle   dba             504 Jan 10 14:28 WAWF_I_002-TEST-0002.xml
-rw-r--r--    1 oracle   dba             504 Jan 10 14:28 WAWF_I_002-TEST-0001.xml
==>
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hello, I tried this solution and I get the following when creating my m4 test file:

-bash: [: i: integer expression expected

Do I need to add something to my logic piece? Also, I noticed this is using SED. Does SED work as well as AWK?

Thanks,
Gilmar
 
Replace this:
while [ i -le max ]
with this:
while [ $i -le $max ]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks so much that seems to work! I successfully created 4 files. I should be able to use this adding a few modifications.

Thanks again,
Gilmar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top