UNIX/PERL script to convert XML file to pipe delimited format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX/PERL script to convert XML file to pipe delimited format
# 8  
Old 11-03-2015
Expertise DON, AIA, RudiC thanks for your inputs and Ideas. I will work on and will let you know the output.. I will post again if i face any issues.. Thanks once again dear's......

---------- Post updated 11-03-15 at 06:25 PM ---------- Previous update was 11-02-15 at 09:11 PM ----------

Hello Aia,

I'm getting output written into a file when i execute the below from command line
"perl my.pl file.xml > output.txt". But this command is not getting executed if i pass this command via a ksh script. I need the output to be written in a file.

Moreover for the "LastSignOn" i need only the date in the format YYYYMMDD. Could you please advise on the same.
# 9  
Old 11-03-2015
Quote:
Originally Posted by karthi1305561
Hello Aia,

I'm getting output written into a file when i execute the below from command line
"perl my.pl file.xml > output.txt". But this command is not getting executed if i pass this command via a ksh script. I need the output to be written in a file.

Moreover for the "LastSignOn" i need only the date in the format YYYYMMDD. Could you please advise on the same.
How are you invoking it inside the ksh script? Could you show it?

Make the following modifications to accommodate the new date format requirement.

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $fname = $ARGV[0] or die $!;
open my $in, '<', $fname or die $!;

my $count = 0;
my %status = ("DISABLED" => "D", "ENABLED" => " ",);
$/ = "<\/ns5:OperatorDefn>";

print "20151027 GLOBAL USER GROUP\n";

while(<$in>) {
     my %record = ();
     while(/<((?:ns[35]:)?(?:Name|EnableStatus|LastSignOn))>(.*)<\/\1>/g){
         push @{$record{$1}}, $2;
     }

     for my $profile (@{$record{"ns3:Name"}}){
        #$record{"ns5:LastSignOn"}->[0] = "" if $record{"ns5:LastSignOn"}->[0] eq "n/a";

         printf "ACR|%s|%s|%s|%s|%s\n", $record{"Name"}->[0],
                                        $profile,
                                        $status{$record{"ns5:EnableStatus"}->[0]},
                                        format_day($record{"ns5:LastSignOn"}->[0]),
                                        $record{"Name"}->[0];
        $count++;
     }
}
close $in;

print "NUMBER OF DETAIL RECORDS: $count\n";

# subroutine to re-arrange date (naive)
sub format_day {
    my $date = shift;
    my @date_bits = split '/', $date;
    if (scalar @date_bits != 3){
        return "";
    }
    $date = join '/', ($date_bits[2]+2000, @date_bits[1,0]);
    return $date;
}

# 10  
Old 11-03-2015
Code:
#!/usr/bin/ksh
BIN_DIR=$HOME/scripts
DATA_DIR=$HOME/scripts/config/data
CONFIG_DIR=$HOME/scripts/config
FILE=$DATA_DIR/SAMPLE.xml
DATE=`date -u +%Y%m%d`
MAILUSERS="abcd@gmail.com"
cd $DATA_DIR
touch SAMPLE_Query_$DATE.log
sample_query -operator -outputfile $FILE >> SAMPLE_Query_$DATE.log
if [ $? -eq 0 ]; then
   echo "SAMPLE Operator Report Created Successfully at `date`" >> SAMPLE_Query_$DATE.log
else
   echo "Error in creating SAMPLE Operator FILE" | mail -s "report failed @ $(hostname) at `date`" $MAILUSERS >> SAMPLE_Query_$DATE.log
exit
fi
#find  $DATA_DIR/SAMPLE_Query*.log -mtime +2 -exec rm {} \; -print >> SAMPLE_Query_$DATE.log
perl $BIN_DIR/karthi.pl $FILE > $DATA_DIR/ZA1P.NDM.SAMPLE.INTERFACE.GRP(+1)

In the above script i'm passing the perl script command and using the symbol ">" to redirect the output.

Last edited by Don Cragun; 11-04-2015 at 03:57 AM.. Reason: Add CODE tags.
# 11  
Old 11-03-2015
Code:
perl $BIN_DIR/karthi.pl $FILE > $DATA_DIR/ZA1P.NDM.SAMPLE.INTERFACE.GRP(+1)

What do you think the highlighted red does? Does your script not bark at you a syntax error?

Please, try:
Code:
perl $BIN_DIR/karthi.pl $FILE > $DATA_DIR/ZA1P.NDM.SAMPLE.INTERFACE.GRP.sample


Last edited by Aia; 11-03-2015 at 07:18 PM..
# 12  
Old 11-03-2015
Yep, thanks that is working. A last correction i need is i don't need the date separate "/" in Lastsignon. YYYYDDMM without "/" is fine..

---------- Post updated at 11:52 PM ---------- Previous update was at 11:35 PM ----------

I'm getting the below error but output is created.

Argument "12 22:14:36" isn't numeric in addition (+) at /home/all_adm/scripts/karthi.pl line 73, <$in> chunk 7.
Argument "12 21:52:45" isn't numeric in addition (+) at /home/all_adm/scripts/karthi.pl line 73, <$in> chunk 18.
Argument "12 04:31:52" isn't numeric in addition (+) at /home/all_adm/scripts/karthi.pl line 73, <$in> chunk 28.
# 13  
Old 11-03-2015
Quote:
Originally Posted by karthi1305561
Yep, thanks that is working. A last correction i need is i don't need the date separate "/" in Lastsignon. YYYYDDMM without "/" is fine..

---------- Post updated at 11:52 PM ---------- Previous update was at 11:35 PM ----------

I'm getting the below error but output is created.

Argument "12 22:14:36" isn't numeric in addition (+) at /home/all_adm/scripts/karthi.pl line 73, <$in> chunk 7.
Argument "12 21:52:45" isn't numeric in addition (+) at /home/all_adm/scripts/karthi.pl line 73, <$in> chunk 18.
Argument "12 04:31:52" isn't numeric in addition (+) at /home/all_adm/scripts/karthi.pl line 73, <$in> chunk 28.

In your original input the only example for `LastSignOn' was <ns5:LastSignOn>18/10/15</ns5:LastSignOn>, the others were `n/a' and they get converted to empty, as your output requirement.
Therefore the format expected in my code is DD/MM/YY which it gets rearranged as 2015/10/18; 15 getting added to the millennia we live in, as a bias.

It appears that your current input `LastSignOn' does not follow the original posted example, and therefore the code fails to do a proper date format. In order to fix it, the subroutine format_day() requires to know every possible format that the xml file is going to yield as a date.
# 14  
Old 11-04-2015
Apologies, My LastSignOn tag has Date & Time. My requirement is only date in the format YYYYDDMM without any separator. Only 8 characters are allowed.
Code:
<ns5:LastSignOn>27/05/14 16:50:52</ns5:LastSignOn>


Last edited by Don Cragun; 11-04-2015 at 04:00 AM.. Reason: Change BOLD tags to CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux convert Comma delimited file to pipe

I have file in linux with comma delimited and string fields in double quotations ", I need to convert them to pipe delimiter please share your inputs. Example: Input: "2017-09-30","ACBD,TVF","01234",NULL,18,NULL,"686091802","BANK OF ABCD, LIMITED, THE",790456 Output: ... (4 Replies)
Discussion started by: shieksir
4 Replies

2. Shell Programming and Scripting

Convert pipe demilited file to vertical tab delimited

Hi All, How can we convert pipe delimited ( or comma ) file to vertical tab (VT) delimited. Regards PK (4 Replies)
Discussion started by: prasson_ibm
4 Replies

3. UNIX for Advanced & Expert Users

Convert CSV file to nested XML file using UNIX/PERL?

we have a CSV which i need to convert to XML using Perl or Unix shell scripting. I was able to build this XML in oracle database. However, SQL/XML query is running for long time. Hence, I'm considering to write a Perl or shell script to generate this XML file. Basically need to build this XML... (3 Replies)
Discussion started by: laknar
3 Replies

4. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

5. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

6. Shell Programming and Scripting

Convert CSV file (with double quoted strings) to pipe delimited file

Hi, could some help me convert CSV file (with double quoted strings) to pipe delimited file: here you go with the same data: 1,Friends,"$3.99 per 1,000 listings",8158here " 1,000 listings " should be a single field. Thanks, Ram (8 Replies)
Discussion started by: Ram.Math
8 Replies

7. UNIX for Dummies Questions & Answers

How to convert a text file into tab delimited format?

I have a text file that made using text editor in Ubuntu. However the text file is not being recognized as space or tab delimited, the formatting seems to be messed up. How can I convert the text file into tab delimited format? (3 Replies)
Discussion started by: evelibertine
3 Replies

8. UNIX for Advanced & Expert Users

Urgent! need help! how to convert this file into comma delimited format

Hi experts, I need urget help! I have the a text file with this format: Types of fruits Name of fruits 1,1 Farm_no,1 apple,1 pineapple,1 grapes,1 orange,1 banana,1 2,2--->this is the record seperator Farm_no,2 apple,1 pineapple,1 grapes,3 orange,2 banana,1 3,3--->this is the... (2 Replies)
Discussion started by: natalie23
2 Replies

9. Shell Programming and Scripting

how to convert this file into comma delimited format

Hi experts, I need urget help! I have the a text file with this format: Types of fruits Name of fruits 1,1 Farm_no,1 apple,1 pineapple,1 grapes,1 orange,1 banana,1 2,2--->this is the record seperator Farm_no,2 apple,1 pineapple,1 grapes,3 orange,2 banana,1 3,3--->this is the... (1 Reply)
Discussion started by: natalie23
1 Replies

10. Shell Programming and Scripting

convert a pipe delimited file to a':" delimited file

i have a file whose data is like this:: osr_pe_assign|-120|wg000d@att.com|4| osr_evt|-21|wg000d@att.com|4| pe_avail|-21|wg000d@att.com|4| osr_svt|-11|wg000d@att.com|4| pe_mop|-13|wg000d@att.com|4| instar_ready|-35|wg000d@att.com|4| nsdnet_ready|-90|wg000d@att.com|4|... (6 Replies)
Discussion started by: priyanka3006
6 Replies
Login or Register to Ask a Question