Sponsored Content
Top Forums Shell Programming and Scripting Filename from splitting files to have the same filename of the original file with counter value Post 302370590 by natalie23 on Thursday 12th of November 2009 01:11:05 AM
Old 11-12-2009
Filename from splitting files to have the same filename of the original file with counter value

Hi all,
I have a list of xml file. I need to split the files to a different files when see the <ko> tag.

The list of filename are
B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml
B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml
B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml

I have a question on how to have the same filename with the counter number append at the end on the filename?

This is the code that I used


perl -n -e '/^<ko>/ and open FH, ">output".$n++; print FH;'

and the filename I get just
output0
output1
output2
output3
output4

Actually,the filename that I needed:

B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml_output0
B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml_output1
B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml_output2
B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml_output3
B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml_output4

B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml_output0
B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml_output1
B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml_output2
B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml_output3
B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml_output4

B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml_output0
B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml_output1
B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml_output2
B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml_output3
B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml_output4

How do modify this code "perl -n -e '/^<ko>/ and open FH, ">output".$n++; print FH;' " so that I can have the filename that I want?
Anyone have any idea?

Thanks in advance!

Last edited by natalie23; 11-12-2009 at 02:29 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Moving files by splitting the path embedded in the filename

Hello All. I am having a directory /tmp/rahul which contains many files in the format @#home@#rahul@#programs@#script.pl where /home/rahul/programs is the directory where the script.pl file is to be placed. I have many files in this format. What i want is a script which read these... (7 Replies)
Discussion started by: rahulrathod
7 Replies

2. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

3. UNIX for Dummies Questions & Answers

Replace all files with a certain filename with another file

I tried searching for this, but I might have used the wrong terms as I couldn't find answers to this question. I'm looking for a way to replace all files with a certain filename with another file within a specific directory including all of it's subdirectory using a shell-script. (2 Replies)
Discussion started by: Schmellsera
2 Replies

4. Shell Programming and Scripting

joining multiple files into one while putting the filename in the file

Hello, I know how to join multiple files using the cat function. I want to do something a little more advanced. Basically I want to put the filename in the first column... One thing to note is that the file is tab delimited. e.g. file1.txt joe 1 4 5 6 7 3 manny 2 3 4 5 6 7 ... (4 Replies)
Discussion started by: phil_heath
4 Replies

5. Shell Programming and Scripting

Merge CSV files and create a column with the filename from the original file

Hello everyone!! I am not completely new to shell script but I havent been able to find the answer to my problem and I'm sure there are some smart brains here up for the challenge :D. I have several CSV files that I need to combine into one, but I also need to know where each row came from.... (7 Replies)
Discussion started by: fransanchezoria
7 Replies

6. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

7. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

8. Shell Programming and Scripting

Non trivial file splitting, saving with variable filename

Hello, Although I have found similar questions, I could not find advice that could help with our problem. The issue: We have a few thousands text files (books). Each book has many chapters. Each chapter is identified by a cite-key. We need to split each of those book files by... (4 Replies)
Discussion started by: samask
4 Replies

9. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

10. UNIX for Beginners Questions & Answers

Renaming files by appending string from within file to filename

Greetings. I am working in a Linux environment and am trying to figure out a way to rename files in a directory by appending a unique strings that appears within a certain area in those files. I have gotten as far as identifying what that particular unique string is with a command like the... (10 Replies)
Discussion started by: HLee1981
10 Replies
XML_PP(1p)						User Contributed Perl Documentation						XML_PP(1p)

NAME
xml_pp - xml pretty-printer SYNOPSYS
xml_pp [options] [<files>] DESCRIPTION
XML pretty printer using XML::Twig OPTIONS
-i[<extension>] edits the file(s) in place, if an extension is provided (no space between "-i" and the extension) then the original file is backed-up with that extension The rules for the extension are the same as Perl's (see perldoc perlrun): if the extension includes no "*" then it is appended to the original file name, If the extension does contain one or more "*" characters, then each "*" is replaced with the current filename. -s <style> the style to use for pretty printing: none, nsgmls, nice, indented, record, or record_c (see XML::Twig docs for the exact description of those styles), 'indented' by default -p <tag(s)> preserves white spaces in tags. You can use several "-p" options or quote the tags if you need more than one -e <encoding> use XML::Twig output_encoding (based on Text::Iconv or Unicode::Map8 and Unicode::String) to set the output encoding. By default the original encoding is preserved. If this option is used the XML declaration is updated (and created if there was none). Make sure that the encoding is supported by the parser you use if you want to be able to process the pretty_printed file (XML::Parser does not support 'latin1' for example, you have to use 'iso-8859-1') -l loads the documents in memory instead of outputing them as they are being parsed. This prevents a bug (see BUGS) but uses more memory -f <file> read the list of files to process from <file>, one per line -v verbose (list the current file being processed) -- stop argument processing (to process files that start with -) -h display help EXAMPLES
xml_pp foo.xml > foo_pp.xml # pretty print foo.xml xml_pp < foo.xml > foo_pp.xml # pretty print from standard input xml_pp -v -i.bak *.xml # pretty print .xml files, with backups xml_pp -v -i'orig_*' *.xml # backups are named orig_<filename> xml_pp -i -p pre foo.xhtml # preserve spaces in pre tags xml_pp -i.bak -p 'pre code' foo.xml # preserve spaces in pre and code tags xml_pp -i.bak -p pre -p code foo.xml # same xml_pp -i -s record mydb_export.xml # pretty print using the record style xml_pp -e utf8 -i foo.xml # output will be in utf8 xml_pp -e iso-8859-1 -i foo.xml # output will be in iso-8859-1 xml_pp -v -i.bak -f lof # pretty print in place files from lof xml_pp -- -i.xml # pretty print the -i.xml file xml_pp -l foo.xml # loads the entire file in memory # before pretty printing it xml_pp -h # display help BUGS
Elements with mixed content that start with an embedded element get an extra <elt><b>b</b>toto<b>bold</b></elt> will be output as <elt> <b>b</b>toto<b>bold</b></elt> Using the "-l" option solves this bug (but uses more memory) TODO
update XML::Twig to use Encode with perl 5.8.0 AUTHOR
Michel Rodriguez <mirod@xmltwig.com> perl v5.12.4 2011-05-18 XML_PP(1p)
All times are GMT -4. The time now is 05:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy