The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Text formatting in Perl. fenox Shell Programming and Scripting 2 02-15-2008 12:06 PM
Perl Sort on Text File eltinator Shell Programming and Scripting 6 08-07-2007 11:20 AM
Formatting a text file based on newline and delimiter characters ntekupal Shell Programming and Scripting 5 05-11-2007 12:33 PM
Perl text file Sn33R Shell Programming and Scripting 5 10-31-2003 07:59 AM
Perl: taking text from a .txt file perleo Shell Programming and Scripting 2 06-19-2003 07:23 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-20-2007
Registered User
 

Join Date: Feb 2007
Posts: 6
Stumble this Post!
text file formatting by perl

have a simple text file as input.i have to print that file in paragraph format.whenevr it finds "\n" in the input text it should start printing in next paragraph in output file.also a fixed amount of space should be given before start writing in every paragraph.
the input and output file format are attached below...
pls provide a unix ar perl script to do this...
Attached Files
File Type: txt good.txt (259 Bytes, 7 views)
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-20-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,572
Stumble this Post!
What have you tried so far ?

Code:
#! /opt/third-party/bin/perl

my($data);

open ( FILE, "< inputfile " ) || die "Unable to open file 'inputfile' <$!> \n";
open ( OUTFILE, "> outputfile " ) || die "Unable to open file 'outputfile' <$!> \n";

while ( read(FILE, $data, 1) == 1 ) {
  if( $data eq "\\" ) {
    if( read(FILE, $data, 1) == 1 ) {
      if( $data eq "n" ) {
        print OUTFILE "\n";
        next;
      }
      else {
        print OUTFILE "\\$data";
        next;
      }
    }
    else {
      last;
    }
  }
  print OUTFILE "$data";
}

close (FILE);
close (OUTFILE);

exit 0
Reply With Quote
  #3 (permalink)  
Old 02-20-2007
Registered User
 

Join Date: Feb 2007
Posts: 6
Stumble this Post!
thanks..workin(but a little more plss..)

many many thanks....


now can you give me solution how to print the output...

the output file always should have a fixed width.means the space for characters in each line should be of same length
output file format is attached.(take it just as example..it should be like this)

look at the 2nd and 3rd line where words r still not completed but the remaining part is written in next line to maintain the fixed line width of output file..

can u help me in this regards...what else to join in ur script.
Attached Files
File Type: txt output.txt (149 Bytes, 4 views)
Reply With Quote
  #4 (permalink)  
Old 02-20-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,572
Stumble this Post!
Code:
#! /opt/third-party/bin/perl

my($data, $MAXCHAR_IN_LINE, $cnt);

$MAXCHAR_IN_LINE = 10;

open ( FILE, "< a" ) || die "Unable to open file 'a' <$!> \n";
open ( OUTFILE, "> b" ) || die "Unable to open file 'b' <$!> \n";

while ( read(FILE, $data, 1) == 1 ) {
  if( $cnt == $MAXCHAR_IN_LINE ) {
    $cnt = 0;
    print OUTFILE "\n";
  }
  else {
    $cnt++;
  }
  if( $data eq "\\" ) {
    if( read(FILE, $data, 1) == 1 ) {
      if( $data eq "n" ) {
        print OUTFILE "\n";
        next;
      }
      else {
        print OUTFILE "\\$data";
        next;
      }
    }
    else {
      last;
    }
  }
  print OUTFILE "$data";
}

close (FILE);
close (OUTFILE);

exit 0
Maximum number of characters in a line is configured in the variable MAXCHAR_IN_LINE

Reply With Quote
  #5 (permalink)  
Old 02-20-2007
Registered User
 

Join Date: Feb 2007
Posts: 6
Stumble this Post!
Smile thanks

many many thanks...its workin well
Reply With Quote
  #6 (permalink)  
Old 02-20-2007
Registered User
 

Join Date: Feb 2007
Posts: 6
Stumble this Post!
Smile thanks

many many thanks...its workin well
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0