Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append the next line 2

Status
Not open for further replies.

TechMaria

Technical User
Jun 14, 2004
6
US
Hi,

I have a file with following info:


AAAAA1 BBBBB1 CCCCC1
AAAAA2 BBBBB2
CCCCC2
AAAAA3 BBBBB3 CCCCC3
AAAAA4 BBBBB4
CCCCC4
AAAAA5 BBBBB5
CCCCC5
AAAAA6 BBBBB6 CCCCC6


I want it to be formatted as follows :

AAAAA1 BBBBB1 CCCCC1
AAAAA2 BBBBB2 CCCCC2
AAAAA3 BBBBB3 CCCCC3
AAAAA4 BBBBB4 CCCCC4
AAAAA5 BBBBB5 CCCCC5
AAAAA6 BBBBB6 CCCCC6

That is, if the 3rd column on a line is null, then append the first word of the next line to it ...

Any help is most appreciated !

Thanx

-Maria
 
maria
that is not easy to do with regexpr
you need a own prog, something like
(awk, NOT TESTED + i guess, does NOT work)

if (NR >2){ last = ""; print; next; }
if (last != ""){ print last,$0; last = ""; next;}
last = $0;


don't forget, RTFMP :) guggach
 
maria: very interesting question.
try to post it in awk, perl and other forum, i believe
only 'c' or better can help you. it's interessant
what other think about this. here my 'c'
download the code in this-file.c
get a compiler, then cc -o doit this-file.c
and run in typing: doit < your-inputfile
----------the code
#include <stdio.h>
#define WORDS 3

int main(int argc,char **argv)
{
register int input, words, last;
last = words = 0;
while((input = getchar()) != EOF){
if(input == '\n'){
if(WORDS == words){ words = 0; putchar(input); }
continue;
}
if(isspace(input) && !isspace(last) ||
!isspace(input) && !words ) ++words;
putchar(input);
last = input;
}
if(words && WORDS != words) putchar('\n');
exit(0);
}


don't forget, RTFMP :) guggach
 
Try...

awk '$3;!$3{printf "%-10s%-12s",$1,$2;getline;print $1}' file
 
Try this ...

cat test|awk '{if(NF==2)lv=$1" "$2;else{print lv,$0;lv=""}}'

Formating needs a bit of attention, but seems to work

Regards,

Martin
 
martin
why, with you a lot other people, did not
(maybe never will) check:
awk is able to read a file, no need of cat ?

don't forget, RTFMP :) guggach
 
Ygor your: $3;!$3 simply brilliant

awk '$3;!$3{print $1,$2;getline;}' file

one small Q
what if the last line contains less then 3 char?

don't forget, RTFMP :) guggach
 
Thanx a great lot for the responses ...

Martin's solution worked the best ...Ygor's solution too worked when the file was exactly the same as given above ..
but in some of my files, there were DDDDD1, EEEEE1 after the CCCCC1 ... and then Ygor's solution had to be modified ...

And Martin's solution worked without any modifications, so I would like to accept his answer...(maybe I'll try converting that to use only awk as guggach suggested !)

guggach, I was more interested in one-line solutions ...

Thanx a lot again ...

-Maria
 
OK Maria
I am more interested in a working solutions.



don't forget, RTFMP :) guggach
 
Maria

Please don't shout at me... i've merely done this for myself!

I know you want a one-liner - I reckon this Perl script with it's regex could be adapted to sed!?

I'm not experienced in UNIX but i'm trying!!!

Code:
#!/usr/bin/perl

undef $/;

$_ = <DATA>;

s|\s*\n\s+|      |g;

print;

__DATA__
AAAAA1    BBBBB1      CCCCC1
AAAAA2    BBBBB2 
                      CCCCC2
AAAAA3    BBBBB3      CCCCC3
AAAAA4    BBBBB4      
                      CCCCC4
AAAAA5    BBBBB5 
                      CCCCC5
AAAAA6    BBBBB6      CCCCC6

output:-

[red][tt]AAAAA1 BBBBB1 CCCCC1
AAAAA2 BBBBB2 CCCCC2
AAAAA3 BBBBB3 CCCCC3
AAAAA4 BBBBB4 CCCCC4
AAAAA5 BBBBB5 CCCCC5
AAAAA6 BBBBB6 CCCCC6[/tt][/red]


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top