Merge broken lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge broken lines
# 8  
Old 04-13-2012
Try next program:
Code:
$ cat script.pl
use warnings;
use strict;

die qq[No argument\n] unless @ARGV == 1;

my @prev_line;

while ( <> ) {
        chomp;

        if ( /\A\*\*/ .. eof ) {
                next if m/\A\s*\Z/;
                if ( m/¦/ ) {
                        if ( @prev_line ) {
                                printf qq[%s\n], pop @prev_line;
                        }
                        push @prev_line, $_;
                        next;
                }

                printf qq[%s\n], (pop @prev_line) . $_;
                next;
        }

        printf qq[%s\n], $_;
}
$ perl script.pl infile
(Output supressed)

# 9  
Old 04-16-2012
Thanks a lot birei.. It works great Smilie But can this be done using shell script instead of perl?
# 10  
Old 04-16-2012
Sorry. I can't help with that. My knowledge of shell script is not enougth for that task.
# 11  
Old 04-16-2012
No problem... Thanks a lot.. But can the perl script make changes to the same file which it is taking as input? I mean i dont want to put it on standard output.. Would like to update the original file itself...
# 12  
Old 04-16-2012
Yes.

Run it like:
Code:
$ perl -i.bak script.pl infile

To change infile in place and make a backup as infile.bak

Do it like:
Code:
$ perl -i script.pl infile

To change infile in place without a backup. Take care with this option.
This User Gave Thanks to birei For This Post:
# 13  
Old 04-16-2012
Thanks a lot for the help Smilie

---------- Post updated at 05:24 PM ---------- Previous update was at 03:38 PM ----------

HI Birei,

I edited it little bit, the script i mean.. i need to read the files from a particular directory and edit the way i told u..
grep is the directory. It should edit each of the files in this directory. Is the script correct. But its not editing the files at all. Could you just check?

Code:
#!/usr/bin/perl -w

use warnings;
use strict;

my @files;
my $file;
@files = </tmp/grep/*>;

foreach $file (@files) {


my @lines;
my @prev_line;
printf $file;

open (FILE,"+>>$file") or die "Can't open $file: $!\n";

@lines= <FILE>;

while ( @lines) {
        chomp;

        if ( /\A\*\*/ .. eof ) {
                next if m/\A\s*\Z/;
                if ( m/¦/ ) {
                        if ( @prev_line ) {
                                printf qq[%s\n], pop @prev_line;
                        }
                        push @prev_line, $_;
                        next;
                }

                printf qq[%s\n], (pop @prev_line) . $_;
                next;
        }

        printf qq[%s\n], $_;
}

  print $file . "Processed\n";
}


i was using this command from grep dir.
Code:
perl -i ../script2.pl


Last edited by Scrutinizer; 04-16-2012 at 08:57 AM.. Reason: code tags
# 14  
Old 04-16-2012
I found a small error in my program (in red the fix), but there is no need to modify it to process many files:
Code:
$ cat script.pl 
use warnings;
use strict;

die qq[No argument\n] if @ARGV == 0;

my @prev_line;

while ( <> ) {
        chomp;

        if ( /\A\*\*/ .. eof ) {
                next if m/\A\s*\Z/;
                if ( m/¦/ ) {
                        if ( @prev_line ) {
                                printf qq[%s\n], pop @prev_line;
                        }
                        push @prev_line, $_;
                        next;
                }

                printf qq[%s\n], (pop @prev_line || qq[]) . $_;
                next;
        }

        printf qq[%s\n], $_;
}

And run it like:
Code:
$ perl -i.bak script.pl /tmp/grep/*

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Joining broken lines and removing empty lines

Hi - I have req to join broken lines and remove empty lines but should NOT be in one line. It has to be as is line by line. The challenge here is there is no end of line/start of line char. thanks in advance Source:- 2003-04-34024|04-10-2003|Claims|Claim|01-13-2003|Air Bag:Driver;... (7 Replies)
Discussion started by: Jackceasar123
7 Replies

2. Shell Programming and Scripting

Merge Lines

Hello I have an input as below this is test we are( ) one end of description I am looking for output this is test we are () one end of description (2 Replies)
Discussion started by: Tomlight
2 Replies

3. Shell Programming and Scripting

Joining broken lines with awk or perl

Hi, I have a huge file with sql broken statements like: PP3697HB @@@@0 <<<<<<Record has been deleted as per PP3697HB>>>>>> FROM sys.xtab_ref rc,sys.xtab_sys f,sys.domp ur WHE RE rc.milf = ur.milf AND rc.molf = f.molf AND ur.dept = 'SWIT'AND ur .department = 'IND' AND share = '2' AND... (4 Replies)
Discussion started by: som.nitk
4 Replies

4. Shell Programming and Scripting

Merge lines

Hello I have a file with CAR 23 COLOR 12 CAR 44 COLOR 12 CAR 55 COLOR 20 SIZE BIG CAR 56 CAR 57 COLOR 11 How can merge the CAR and the COLOR + SIZE (if there are COLOR,SIZE) CAR 23 COLOR 12 CAR 44 COLOR 12 CAR 55 COLOR 20 SIZE BIG CAR 56 CAR 57 COLOR 11 Every line begin in... (4 Replies)
Discussion started by: sharong
4 Replies

5. UNIX for Dummies Questions & Answers

How to identify broken lines in a file?

Hi, I have a 100 byte length fixed width file . In that three rows are broken and went off to next line. How can I identify the broken lines? E.g. ABCD1234MNRD4321 abcd1234mnrd 4321 As you can see in my example my second row with small case alphabets is broken... (5 Replies)
Discussion started by: okkadu
5 Replies

6. Shell Programming and Scripting

Joining broken lines

I have a plain test file with a delimeter ''. In this file some lines are broken into two. The first part of these broken line will have 6 columns and the second part will have 4. These broken lines will be consicutive. I want to join the two consicutive lines which are having 6 fields and 4... (8 Replies)
Discussion started by: ratheeshjulk
8 Replies

7. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

8. Shell Programming and Scripting

Merge two lines

Hi I have two lines of data formatted as displayed below shop, price, remark, date a,#N/A,order to -fd, 20091011 and would like it to be shop:a price:#N/A remark:order to -fd date:20091011 How can I do it? Many thanks (2 Replies)
Discussion started by: lalelle
2 Replies

9. Shell Programming and Scripting

awk / shell - Fix broken lines and data

Gurus, I am struggling with a issue and thought I could use some of your expertise. Need Help with this I have a flat file that has millions of records 24|john|account ~ info |56| 25|kuo|account ~ journal |58| 27|kim|account ~ journal |59| 28|San|account ~ journal |60|... (3 Replies)
Discussion started by: rimss
3 Replies
Login or Register to Ask a Question