I am writing a Powershell script to split one big file into multiple files with two pairs of a tag each in a file and those small filenames must follow a naming convention.
Example abcdef123.xml
contents:
<parent>
<child>
<code1><code1>
<text1><text1>
</child>
<child1>
<code2><code2>
<text2><text2>
</child1>
<child>
<code3><code3>
<text3><text3>
</child>
<child1>
<code4><code4>
<text4><text4>
</child1>
<child>
<code5><code5>
<text5><text5>
</child>
<child1>
<code6><code6>
<text6><text6>
</child1>
<child>
<code7><code7>
<text7><text7>
</child>
<child1>
<code8><code8>
<text8><text8>
</child1>
</parent>
The Powershell script should split this big file into multiple files (with 2 pairs of <child>
& <child1>
each in the file) having the following criteria and take user input for file name convention (the date with miliseconds can remain same in all file name but variable j
should change):-
Criteria:-
- Add header
<parent>
and tail</parent>
to each file. - File name should be in the format of
UserinputstringMMDDYYYYHHMMSSMIL_n increment.xml
(whereMIL
is milliseconds andn increment
will be like001
,002
,003
, ...) - No two files should have the same filename.
Example file splits:-
file 1; stack_10132020134434789_001.xml
contents:
<parent>
<child>
<code1><code1>
<text1><text1>
</child>
<child1>
<code2><code2>
<text2><text2>
</child1>
<child>
<code3><code3>
<text3><text3>
</child>
<child1>
<code4><code4>
<text4><text4>
</child1>
</parent>
file 2; stack_10132020134434791_002.xml
contents:
<parent>
<child>
<code5><code5>
<text5><text5>
</child>
<child1>
<code6><code6>
<text6><text6>
</child1>
<child>
<code7><code7>
<text7><text7>
</child>
<child1>
<code8><code8>
<text8><text8>
</child1>
</parent>
Script I was trying:
csplit -ksf part. src.xml
n=000
#E.g. Enter beginning of file name :
#User entered-> stack
#read userinput
j=n+1
$date= date +%m%d%Y%H%M%S%3N
filename=$userinput$date_$j.xml