Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Shell script change new format on the file.

Shell Programming and Scripting


Tags
solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 06-10-2017   -   Original Discussion by dotran
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 Posts
Quote:
Originally Posted by dotran View Post
[...]
---------- Post updated at 08:39 PM ---------- Previous update was at 05:33 PM ----------

I have this perl code and when run see extra "|" at the end of each line. And not sure how to code remove it or can write shell script easier.....can someone help? Thanks

Code:
 
 #!/usr/bin/perl 
 my $filename = 'abc.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)  or die "Could not open file '$filename' $!";
print "FILE_NAME,Filename|Data Format|Compression|GZIP Bytes|Unzipped Bytes|Records|Record Length";
while (my $row = <$fh>) {
  chomp $row;
  my ($label, $value) = split /: /, $row;
  if ($row eq '') {
    print "\n";
  }
  else
  {
     print "$value|";
  }
}

/test1> ./test.pl

Code:
FILE_NAME,Filename|Data Format|Compression|GZIP Bytes|Unzipped Bytes|Records|Record Length|||||
SW_PP_CTRL_20170505.txt.gz|ASCII with carriage returns and linefeeds|GZIP|2019064|11413730|95788|157|
SW_PP_DATA_20170505.txt.gz|ASCII with carriage returns and linefeeds|GZIP|691778058|8316153069|60400481|158|
SW_PP_DEMO_20170505.txt.gz|ASCII with carriage returns and linefeeds|GZIP|26240709|77053000|543250|227|
SW_PP_PLANXREF_20170505.txt.gz|ASCII with carriage returns and linefeeds|GZIP|557904|3061930|16262|310|
SW_PP_PRODUCT_20170505.txt.gz|ASCII with carriage returns and linefeeds|GZIP|21375|229431|1264|211|
SW_PP_REASSIGN_20170505.txt.gz|ASCII with carriage returns and linefeeds|GZIP|32681|69399|802|130|


A possible fix.


Code:
#!/usr/bin/perl
my $filename = 'abc.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)  or die "Could not open file '$filename' $!";
print "FILE_NAME,Filename|Data Format|Compression|GZIP Bytes|Unzipped Bytes|Records|Record Length\n";
my $tmp;
while(<$fh>){
  
  if(/^Filename/../^Record L/){
    my $value = (split /:\s+/)[1];
    chomp $value;
    if($tmp) {
      $tmp .= "|$value";
    }
    else {
      $tmp .= "$value";
    }
    if(/^Record L/){
      print "$tmp\n";
      undef $tmp;
    }
  }
}
close $fh;

Slighter improved version.


Code:
#!/usr/bin/perl
my $filename = 'abc.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)  or die "Could not open file '$filename' $!";
print "FILE_NAME,Filename|Data Format|Compression|GZIP Bytes|Unzipped Bytes|Records|Record Length\n";
my @tmp;
$" = '|';
while(<$fh>){
  if(/^Filename/../^Record L/){
    my $value = (split /:\s+/)[1];
    chomp $value;
    push @tmp, $value;
    if(/^Record L/){
      print "@tmp\n";
      undef @tmp;
    }
  }
}
close $fh;

Another version that dynamically takes care of the header.

Run as perl transform.pl example_file

Code:
#!/usr/bin/perl
use strict;
use warnings;

my @tmp;
my @header;
my $build_header = 1;
$"="|";
while(<>){
  if(/^Filename/../^Record L/){
    my ($id, $value) = split /\s+:\s+|\n/;
    push @tmp, $value;
    push @header, $id if $build_header;
    if($id =~ /^Record L/){
      print "FILE_NAME,@header\n" if $build_header;
      print "@tmp\n";
      undef @tmp;
      undef $build_header;
    }
  }
}


Last edited by Aia; 06-10-2017 at 12:32 AM.. Reason: Add another example
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Change date format in shell script shivasaini Shell Programming and Scripting 1 01-25-2013 10:50 AM
How to check file name format using shell script? Poonamol Shell Programming and Scripting 7 10-13-2010 08:20 AM
format file using shell script balajim Solaris 2 10-23-2008 10:18 AM
format file using shell script balajim Solaris 1 10-22-2008 04:30 PM
script to change the date format in a file shehzad_m Shell Programming and Scripting 15 06-11-2008 11:30 AM



All times are GMT -4. The time now is 02:30 PM.