text file formatting by perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting text file formatting by perl
# 1  
Old 02-20-2007
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...
# 2  
Old 02-20-2007
What have you tried so far ? Smilie

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

# 3  
Old 02-20-2007
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.
# 4  
Old 02-20-2007
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

Smilie
# 5  
Old 02-21-2007
Bug thanks

many many thanks...its workin well Smilie
# 6  
Old 02-21-2007
Bug thanks

many many thanks...its workin well Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting text file

Hi All, how to format text Thanks (19 Replies)
Discussion started by: ROCK_PLSQL
19 Replies

2. Shell Programming and Scripting

Formatting a text file

Hi All :), I have a formatting question and I am unsure on how I should proceed with my bash shell script. I am unsure weather to use perl or simply edit it in bash. I prefer bash but I am only aware of the awk utility to extract parts of a file, not edit output. Scenario: I have a file... (5 Replies)
Discussion started by: bstrizzy
5 Replies

3. Shell Programming and Scripting

Perl syntax for formatting columns and text

Dear all, Pzl let me know what is the syntax for converting the columns format to text as i have lots of values for but when i put these values in xls sheet the values are automatically converted to and one more question i have is how to call values from shell script into perl script eg. ... (3 Replies)
Discussion started by: sagar_1986
3 Replies

4. Shell Programming and Scripting

[Solved] Formatting the text file

Hi All, I ahve requirement where I want to put the text file in into proper format. I am wondering how can i achieve that:- Host/Alias Name IP Address Resolved sinuiy01.infra.go2uti.com 10.240.8.158 N sinuid20.devtst.go2uti.com 10.240.8.230 N sinuid21.devtst.go2uti.com... (6 Replies)
Discussion started by: sharsour
6 Replies

5. Shell Programming and Scripting

Comparing and Formatting the text file

hi, I need a script which can format the below text file which contains comments file1.txt -------- //START //Name: some value //Date: //Changes:............. //..................... //END //START //Date: //Name: some value //Changes:............. //..................... (3 Replies)
Discussion started by: flamingo_l
3 Replies

6. Shell Programming and Scripting

Formatting the text file using shell script

How to add the filename to end of each line with | as seperator, except first and last line of the file(s) in directories(with diff tree structure) using shell script?. And also how to replace a list of strings with another set of strings, which is present in a file?. Kindly help out on... (1 Reply)
Discussion started by: av_vinay
1 Replies

7. Shell Programming and Scripting

Formatting text file in unix

Hi, I am using the following format command for formatting my text file in unix. awk -F":" '{ printf "%-50s%-1s%-50s\n", $1,":", $2}' filename > targetfile The target file is then sent as an attachment via email. When I view the target file in notepad multiple lines get spanned as a... (2 Replies)
Discussion started by: AAA
2 Replies

8. UNIX for Dummies Questions & Answers

formatting of the text file

Hi Guys, I have a file with contents in the below format DO_VJ_IDOC;03.23.2009;22:31:09; ZJDO_VJ_IDOC;03.23.2009;22:46:14; ZJDO_RESEND_FAILURES;03.24.2009;01:46:18; Now i need to replace the semicolons with tabs for which i am usig the sed command which gives the O/p as below ... (1 Reply)
Discussion started by: rohit.shetty84
1 Replies

9. UNIX for Dummies Questions & Answers

Text file formatting

Hi all! I'm new in unix, and faced with some difficulties. So I have text file f.e. "textfile" which contains rows like: aaa bbb ccc ddd How could I format it, so the file looks like: aaabbb cccddd Thanks in andvance (5 Replies)
Discussion started by: consta.v
5 Replies

10. Shell Programming and Scripting

Text formatting in Perl.

Hiho, I've got a variable $sth = `du -sh /home/$whoami`; where $whoami is actually logged user. I want to have only the size of this directory eg. 2,7G. I know its lame, but anyway.. how to do it? (2 Replies)
Discussion started by: fenox
2 Replies
Login or Register to Ask a Question