Split large xml into mutiple files and with header and footer in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split large xml into mutiple files and with header and footer in file
# 15  
Old 02-07-2019
Hello All.

Code:
$x =sampletest_110.xml
sampletest_111.xml

Command :
Code:
awk -f xml_String_split.awk -f xml_split.awk OUT=$x"" ROWS="500" $x $x

For the above its generating file names as below
Code:
sampletest_110.xml.0001
sampletest_111.xml.0001

when i use mv command to rename the files
I used below command to mv
Code:
for f in sampletest_*; do mv "$f" "${f/sampletest_/Extrfile}.xml";

Output file names:
Code:
Extrfile110.xml.0001.xml
Extrfile111.xml.0001.xml

Iam expecting filenames to be as below please help me to achieve below :
Code:
Extrfile110.xml
Extrfile111.xml

----


Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-07-2019 at 04:29 AM.. Reason: Added CODE tags.
# 16  
Old 02-07-2019
I've got some difficilties understanding what you are trying to do, so maybe just some comments:


  • If you're trying to assign two lines to a variable with
    Code:
    $x =sampletest_110.xml
    sampletest_111.xml

    , you'll have a) a syntax error, b) a logical error and (probably) c) two "command not found" errors. Remove the $sign, the space, and quote the entire string
  • Code:
    awk -f xml_String_split.awk -f xml_split.awk OUT=$x"" ROWS="500" $x $x

    is a very strange construct. If I interpret it correctly, it will run the split script eight times: twice on the first file name in $x, twice on the second, and again for the second instance of $x. Is that what you want?
  • Code:
    for f in sampletest_*; do mv "$f" "${f/sampletest_/Extrfile}.xml";

    (A done is missing here!) You replace the "sampletest" string with "Extrfile", and append ".xml". The result is exactly that. You desired target will be achieved in multiple steps, like
    Code:
    for f in sampletest_*
      do    TMP="${f/sampletest_/Extrfile}"
            echo mv "$f" "${TMP%.*}"
      done


Last edited by RudiC; 02-07-2019 at 07:24 AM..
This User Gave Thanks to RudiC For This Post:
# 17  
Old 02-07-2019
In addition to what RudiC has already said, note that asking us to tell you why the command:
Code:
awk -f xml_String_split.awk -f xml_split.awk OUT=$x"" ROWS="500" $x $x

doesn't work without showing us your code contained in the files xml_split.awk and xml_String_split.awk is rather difficult.

My crystal ball isn't working well enough to spot your problems in these files this morning.
This User Gave Thanks to Don Cragun For This Post:
# 18  
Old 02-07-2019
Thanks Rudic it worked.

--- Post updated at 11:06 PM ---

Hi Rudic,

Need one more help I have a xml file with the below job_id repetition, for every response i get from service it will generate one job_id i will store that in a response.xml file
my requirement is to pick the job_id's and store it in a variable with comma seperator as the below format and pass that variable value to DB as input


Required output:
Code:
30537,30538,30539

XML :
Code:
JobUnique_Id><ns11:Job_Id>30537</ns11:Job_Id></ns6:JobResponse>
             <ns11:Job_Id>30538</ns11:Job_Id
             <ns11:Job_Id>30539</ns11:Job_Id

--- Post updated at 11:08 PM ---

Rudic reply helped me sorry for not briefing you the problem in detail.

Last edited by karthik; 02-07-2019 at 09:04 PM..
# 19  
Old 02-08-2019
My generic XML script includes instructions and examples for extracting data. What have you tried?
# 20  
Old 02-10-2019
Hi Corona,

I have tried the below where as i can find job_id value when trying to replace the job_id values with comma seperator that new_var is blank kindly assist

Code:
JobId =$(cat Response.xml | awk -F"Job_Id>" '{print $2}' | awk -F"<" '{print $1}')
echo $JobId

JOB_ID output here is :12345
23415

Code:
NEW_VAR=$(echo $JobId | sed -e "s/^/'/" | sed -e "s/$/'/" | tr '\n' ',')

Output:12345,23415 is the expected output for the NEW_VAR
# 21  
Old 02-10-2019
Hi karthik,
As usual, I seem to be lost again trying to understand what you are trying to do.
Quote:
Originally Posted by karthik
Hi Corona,

I have tried the below where as i can find job_id value when trying to replace the job_id values with comma seperator that new_var is blank kindly assist

Code:
JobId =$(cat Response.xml | awk -F"Job_Id>" '{print $2}' | awk -F"<" '{print $1}')
echo $JobId

JOB_ID output here is :12345
23415
With a <space> between the first word of the above command and the <equals-sign>, command runs a utility named JobId with one or more arguments (depending on the output from the two awk commands in your script) where the first argument's first character is the <equals-sign>. The echo command after that line will print an empty line unless JobId had been assigned a value somewhere else.

Furthermore, JOB_ID and JobId are not even close to being the same thing AND with no CODE tags I have no idea what value you are saying the output of that awk command was. And, since you haven't shown us any sample file named Response.xml we have no way of recreating the input you are feeding into that script to try it out ourselves.

Quote:
Originally Posted by karthik
Code:
NEW_VAR=$(echo $JobId | sed -e "s/^/'/" | sed -e "s/$/'/" | tr '\n' ',')

Output:12345,23415 is the expected output for the NEW_VAR
The echo of an unquoted variable expansion is only going to produce a single <newline> character at the end of the single line of output it produces; so you can't possibly want tr to change that <newline> character into a <comma>? And, you're invoking sed two more times to enclose your results in <single-quote>s, but you don't show any <single-quote>s in the expected output you say you want to store into the shell variable NEW_VAR. So, again, I'm very confused about how this code might be expected to produce the output you want.

Unless you want to run a loop processing the individual job IDs produce by the first awk script above, why not just have it print the results you want in the format in which you want them to be printed instead of producing output you don't want following by invoking four more utilities to reformat the output?

Making the wild assumption that the awk script you showed us produced two output lines with each containing a single Job ID and that you want to set NEW_VAR to a string containing just those two Job IDs separated by a <comma>, we should be able to produce aMUCH more efficient single awk command to produce the output you want to store into the variable NEW_VAR. But, of course, with no sample input to work with, I can't determine whether the script above that calls awk twice is expecting to find two Job IDs on a single line input file or is expecting to find one Job ID on each of two input lines. Therefore, I can't suggest an awk command that might work for you. Either way there is no need for two awk commands, two sed command, atr command, and two command substitutions.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Eliminate Header and footer from EBCDIC file

Is there any command to eliminate Header and footer from EBCDIC file (4 Replies)
Discussion started by: abhilashnair
4 Replies

2. UNIX for Dummies Questions & Answers

File Row Line Count without Header Footer

Hi There! I am saving the file count of all files in a directory to an output file using: wc -l * > FileCount.txt I get: 114 G4SXORD 3 G4SXORH 0 G4SXORP 117 total But this count includes header and footer. I want to subtract 2 from the count and get ... (7 Replies)
Discussion started by: gagan8877
7 Replies

3. Shell Programming and Scripting

Is there a way to append both at header and footer of a file

currently I've a file Insert into CD_CARD_TYPE (CODE, DESCRIPTION, LAST_UPDATE_BY, LAST_UPDATE_DATE) Values ('024', '024', 2, sysdate); Insert into CD_CARD_TYPE (CODE, DESCRIPTION, LAST_UPDATE_BY, LAST_UPDATE_DATE) Values ('032', '032', 2, sysdate); ........ is it... (3 Replies)
Discussion started by: jediwannabe
3 Replies

4. Shell Programming and Scripting

Removing header or footer from file

Hi Every one, what is the coomand to remove header or footer from a file. Please help me by providing command/syntax to remove header/footer from unix. Thanks in advance for all your support. (5 Replies)
Discussion started by: sridhardwh
5 Replies

5. Shell Programming and Scripting

Add header and footer with record count in footer

This is my file(Target.txt) name|age|locaction abc|23|del xyz|24|mum jkl|25|kol The file should be like this 1|03252012 1|name|age|location 2|abc|23|del 2|xyz|24|mum 2|jkl|25|kol 2|kkk|26|hyd 3|4 Column 1 is row indicator for row 1 and 2, column indicator is 1,for data rows... (1 Reply)
Discussion started by: itsranjan
1 Replies

6. Shell Programming and Scripting

sort a report file having header and footer

I am having report file with header and footer . The details in between header and footer are separated by a pipe charater. I want to sort the file by considering multiple columns in between header and footer. pls help (4 Replies)
Discussion started by: suryanarayana
4 Replies

7. Shell Programming and Scripting

Split large file and add header and footer to each small files

I have one large file, after every 200 line i have to split the file and the add header and footer to each small file? It is possible to add different header and footer to each file? (7 Replies)
Discussion started by: ashish4422
7 Replies

8. Shell Programming and Scripting

Split large file and add header and footer to each file

I have one large file, after every 200 line i have to split the file and the add header and footer to each small file? It is possible to add different header and footer to each file? (1 Reply)
Discussion started by: ashish4422
1 Replies

9. Shell Programming and Scripting

Total of lines w/out header and footer incude for a file

I am trying to get a total number of tapes w/out headers or footers in a ERV file and append it to the file. For some reason I cannot get it to work. Any ideas? #!/bin/sh dat=`date +"%b%d_%Y"` + date +%b%d_%Y dat=Nov16_2006 tapemgr="/export/home/legato/tapemgr/rpts"... (1 Reply)
Discussion started by: gzs553
1 Replies

10. Shell Programming and Scripting

Need to Chop Header and Footer record from input file

Hi, I need to chope the header and footer record from an input file and make a new output file, please let me know how i can do it in unix.thanks. (4 Replies)
Discussion started by: coolbudy
4 Replies
Login or Register to Ask a Question