Operating System - HP-UX
1753556 Members
5702 Online
108796 Solutions
New Discussion юеВ

ed help- join two lines on file based on error log line number

 
Tapas Jha
Valued Contributor

ed help- join two lines on file based on error log line number

Hi Script Expert,

I have one requirement on file manupulation(joing lines based on error log files's line number).

One of my script upload files on database and sometimes this gives
error on log file. The error is looked like below:
847: Error in load file line 1.
Error in line 2
Near character position 16


Now, checking the error log file, i edit the file using vi manually (for thos case join line 1 and line 2, see below sample file). I want this to be automated (without vi).

Please provide any script(Awk, sed, ed anything is welcome, I think ed can help out. ) which will look into the error log file, and checking the line number (Error in load file line 1, Error may happen in any line), join the next line and said line on the file, say DBLOAD.

Sample of the file (assume file name DBLOAD) is in below: ( I have added Line 1--, Line 2--, Line 3--. Line 4-- above of each line of DBLOAD file to understand the line number.)

Line 1--
"F","1","2008/06/18","09:02:59","MA0002","MAA","IN","MAA","2008/06/18","08:27:1
1","kg","","1,2,3","2008/06/18","","INDOXTMPLT000","D,X,B,N,P,U,F,E","",
Line 2--
"M","MA000218068",
Line 3--
"A","1","1","MA000218068","1490401216","R","RTM","NL","VOM","D ","1\ 1","0.1
","DOCUMENTS","","","ABN AMRO BANK NV","MAILROOM","VAN OLDENBARNEVELTPLAATS 22",
"ROTTERDAM","","3012 AH","","","NL","+31205359000","SYNDICATE BANK","","PAVAN CO
MMERCIAL COMPLEX","FL. 1-2 DABAGARDENS 30-15-147","","530020","","","IN","",
Line 4--
"A","1","2","MA000218068","1503152291","R","BSL","CH","VOM","D ","1\ 1","0.1
2","BUSINESS DOCUMENT","","","GLENCORE INTERNATIONAL AG","ZINC + COPPER",".","BA
ARERMATTSTRASSE 3","","6341","","","CH","O41 709 20 00","STERLITE INDUSTRIAL (IN
DIA) LIMITED",".","MADURAI BYPASS ROAD","SIPCOT INDUSTRIAL COMPLEX","","628002",
"","","IN","+91 22 6643 4599",
Tapas Jha
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: ed help- join two lines on file based on error log line number

TMP=/var/tmp/el.$$
awk '
/Error in load file line/ {
# perform concatenation
print "NR ==", substr($NF, 1, length($NF)-1), "{ printf $0; getline }"
}
END { print "{print $0}" } ' errorlog-file > $TMP

cat $TMP # check script to edit DBLOAD

awk -f $TMP DBLOAD > DBLOAD.new

rm -f $TMP
Tapas Jha
Valued Contributor

Re: ed help- join two lines on file based on error log line number

Hi,

I have run the below program as per you like below, however nothing happens.
TMP=/var/tmp/el.$$
awk '/Error in line/ { # perform concat
print "NR==",substr($NF,1,$length($NF)-1),"{ printf $0;getline }"
}

END { print $0 }' errorlog-file > $TMP
cat $TMP
awk -f $TMP DBLOAD >DBLOAD.new
rm -f $TMP
While running i am getting error message as :
syntax error The source line is 1.

The error context is
NR== >>> { <<<

Contents of /var/tmp/el.$$ is NR== {printf $0;getline}
Tapas Jha
Peter Nikitka
Honored Contributor

Re: ed help- join two lines on file based on error log line number

Hi,

I ask myself, if it wouldn't be better to check the file for correctness BEFORE using it.
If there is a fixed number of records needed, this will output all line numbers containing NOT exactly n=36 fields:
awk -F, -v n=36 'NF != n {print NR,NF}' infile

will output (your example):
1 28
2 3

Assuming, your example would show
1 28
2 8
or
1 28
2 3
3 5
i.e. the number of fields of incomplete consequitive lines addup to your field count,
the following would join these lines:

awk -F, -v n=36 '{if(NF+hold > n) { print ("ERROR: overflow in line",NR); hold=0; exit 1}
hold+=NF;hold_str=hold_str""$0
if(hold==n) {print NR,hold_str;hold_str="";hold=0} }
END {if(hold) {print ("ERROR: incomplete file"); exit 1}}' infile

This awk aborts on errors.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: ed help- join two lines on file based on error log line number

Hi:

Here's one way:

# cat joiner
#!/usr/bin/perl
use strict;
use warnings;
my $linenbr = shift;
my $file = shift;
die "Usage $0: linenumber file\n"
unless $file && $linenbr && $linenbr =~ m/^\d+$/;
open( FH, "<", $file ) or die "Can't open '$file': $!\n";
while () {
if ( $. == $linenbr ) {
chomp;
$_ .= ;
redo unless eof(FH);
}
print;
}
1;

...pass the target line-number to which you want to join the successive line along with the name of the file. For example:

# ./joiner 1 log

...would join line-2 to line-1

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: ed help- join two lines on file based on error log line number

Hi,
forgot: to remove the trace of line numbers, change
if(hold==n) {print NR,hold_str;hold_str="";hold=0} }
to
if(hold==n) {print hold_str;hold_str="";hold=0} }

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: ed help- join two lines on file based on error log line number

>I have run the below program as per you like below, however nothing happens.
The error context is
NR== >>> { <<<

This means there is something wrong with your description of your error file.
What is the exact format of this error message?
847: Error in load file line 1.
My script expects it to look like that.