Need shell script version of below perl code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need shell script version of below perl code
# 1  
Old 06-09-2016
Need shell script version of below perl code

We are using AIX version 7100-03-05-1524

Please provide shell script version (bash or ksh) of below perl code,since we need to have line breaks in huge XML files

Code:
#!/usr/bin/perl
#
# Purpose: Read an XML file and indent it for ease of reading
# Author:  RedGrittyBrick 2011. 
# Licence: Creative Commons Attribution-ShareAlike 3.0 Unported License
#
use strict;
use warnings;

my $filename = $ARGV[0];
die "Usage: $0 filename\n" unless $filename;

open my $fh , '<', $filename
  or die "Can't read '$filename' because $!\n";
my $xml = '';
while (<$fh>) { $xml .= $_; }
close $fh;

$xml =~ s|>[\n\s]+<|><|gs;                       # remove superfluous whitespace
$xml =~ s|><|>\n<|gs;                            # split line at consecutive tags

my $indent = 0;
for my $line (split /\n/, $xml) {

  if ($line =~ m|^</|) { $indent--; }

  print '  'x$indent, $line, "\n";

  if ($line =~ m|^<[^/\?]|) { $indent++; }             # indent after <foo
  if ($line =~ m|^<[^/][^>]*>[^<]*</|) { $indent--; }  # but not <foo>..</foo>
  if ($line =~ m|^<[^/][^>]*/>|) { $indent--; }        # and not <foo/>

}


Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 06-09-2016 at 05:07 AM.. Reason: code tags
# 2  
Old 06-09-2016
This forum is no drive-in where you place an order. This is driven by volunteers - not employees that do the job for you. So please form your wording a bit less demanding, thanks!

Last edited by zaxxon; 06-09-2016 at 09:35 AM.. Reason: typos
# 3  
Old 06-09-2016
Zaxxon,sorry if my request sounded like demanding

I don't have knowledge about Perl scripting.Since we wanted to introduce line breaks for HUGE XML,i got this Perl code from internet

can someone guide me the in conversion of the perl code into shell scripting code which holds good for AIX 7.0.0.1 compiler

Sorry again!

I appreciate somebody's help on thisSmilie
# 4  
Old 06-09-2016
(1) What has the programming language to do with having or not having line breaks?

(2) I don't quite get the intention of your request: If you *know*, what your program is supposed to do, why don't you write it your self in the language of your choice (ksh, bash,....)? If you know neither shell scripting NOR Perl, what's the point in rewriting it? It would be easier to ask someone with Perl knowledge to help you with modifying the Perl program, instead to rewrite it from scratch.
# 5  
Old 06-09-2016
Rovf,

We have a hugh size XML ~15 GB(record size is more than 32756 bytes),we want to do ftp the same to mainframe server

The maximum limit record size for a mainframe file is LRECL-32756

In order to reduce the record size of the XML,we are introducing line breaks from the command line and then do the FTP to mainframe server

As we follow shell scripting process in our existing framework, i found this Perl code in internet(Thanks to RedGrittyBrick) which introduces line breaks in an XML file

if the PERL to shell conversion is possible to introduce line breaks from command line that would be highly useful

We also tried with tr and sed commands on XML,but we could not arrive at the expected solution

Thanks again!

---------- Post updated at 05:09 AM ---------- Previous update was at 05:03 AM ----------

Rovf,

We have a hugh size XML ~15 GB(record size is more than 32756 bytes),we want to do ftp the same to mainframe server

The maximum limit record size for a mainframe file is LRECL-32756

In order to reduce the record size of the XML,we are introducing line breaks from the command line and then do the FTP to mainframe server

As we follow shell scripting process in our existing framework, i found this Perl code in internet(Thanks to RedGrittyBrick) which introduces line breaks in an XML file

if the PERL to shell conversion is possible to introduce line breaks from command line that would be highly useful

We also tried with tr and sed commands on XML,but we could not arrive at the expected solution

Thanks again!
# 6  
Old 06-09-2016
Without diving into the logics of the above script, wouldn't a simple
Code:
sed 's/>/>\n/g'

do? Wouldn't be a prettyprint, though.
# 7  
Old 06-09-2016
Below result i'm getting,instead of line breaks it puts simply n

see below example for that command output

Code:
<?xml version="1.0" ?>n<issuer>n

i tried
Code:
sed  -e 's/>/>\n/g'

command also,same result




Moderator's Comments:
Mod Comment NO- NONSENSE: Please use code tags as required by forum rules!

Last edited by RudiC; 06-09-2016 at 07:37 AM.. Reason: Added code tags AND WARNING!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

2. Shell Programming and Scripting

Shell Script for Java version switch

Hello all I am writing a shell script for unix, and I am trying to get it to ask me if instances are stopped, and then if the version of java running is correct, if not I want it to ask me to switch to the other bit version. I am newish to shell and not clear on how to to input prompts for yes/no... (3 Replies)
Discussion started by: bigbenn
3 Replies

3. Shell Programming and Scripting

Portable Shell Script - Determine Which Version of Binary is Installed?

I currently have a shell script that utilizes the "Date" binary - this application is slightly different on OS X (BSD General Commmand) and Linux systems (gnu date). In particular, the version on OS X requires the following to get a date 14 days in the future "date -v+14d -u +%Y-%m-%d" where gnu... (1 Reply)
Discussion started by: colinjohnson
1 Replies

4. Shell Programming and Scripting

How to compare version values in shell script?

Hi, I need to compare two versions values and report only true or false depending on their difference result. like - echo $(echo "1.8 >= 2.0" | bc) 0 echo $(echo "2.0 >= 2.0" | bc) 1 but my task is to compare values like - echo $(echo "1.9.1 >= 2.0" | bc) (standard_in) 1: syntax... (3 Replies)
Discussion started by: abhitanshu
3 Replies

5. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

Version Control Through the Shell Script

Version Control Through the Shell Script Hi Guys, Apologize for the big request, please take some time and read it completely... This is Very important for me, and ur help is Very much Appriciated. I want to maintain the Version control to all my scripts running in Production server, I am... (6 Replies)
Discussion started by: Anji
6 Replies

7. Shell Programming and Scripting

convert perl code to shell script

This is about how to Monitoring folder for new files using shell script im doing a project using smsserver tools 3. i have used a perl script to handle incoming messages. the content of each message must be directed to a java program. this program generates the answer to reply to the user... (2 Replies)
Discussion started by: x34
2 Replies

8. Shell Programming and Scripting

Perl Script Syntax error in version 4

Hi , I use the following simple perl script to find the yesterday time perl -e ' use POSIX(strftime); print POSIX::strftime("%a %b %e %H:%M:%S %Y", localtime(time-86400*$ARGV))' 1 However in the perl version 4 , it gives me the following error : Do the perl version 4 does not support... (4 Replies)
Discussion started by: youareapkman
4 Replies

9. Shell Programming and Scripting

How to run perl code within a shell script...?

Hi, I have a sheel script that invokes a perl script...Now, instead havin the perl script as a separate file I'd like put the contents in the sheel script itself...But I am not sure how ro run that perl script contents.please help me Thanks (1 Reply)
Discussion started by: vijay_0209
1 Replies

10. UNIX for Dummies Questions & Answers

Need Shell script for getting Firefox Browser Version

Hi, How to write a script for finding out firefox version in our linux machine? Could you please share the same? I am using Red Hat Linux machine. Thanks, Kammy (2 Replies)
Discussion started by: kjannu
2 Replies
Login or Register to Ask a Question