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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing files 2

Status
Not open for further replies.

Igaduma

Technical User
Nov 23, 2001
322
BE
Hello All,

I've been busy trying to organize a 'chaotic' file and
after using ex and pattern searching in a bash script I
reached this almost final format.
But, anymore caffeine and I will never ever sleep again.

Any help here...is highly appriciated!

I have a file like this:

RXG-1 55001
RXG-2 55002
RXG-3 55003
RXG-4 55161
55162
55163
RXG-6 55211
55213
RXG-7 55231
55232
55233
RXG-8 55836

<end>

basically 2 columns, but (and you feel it coming:))
I need to take col1 from the previous line IF on the current line col1 is empty and pipe it to a file resulting in
col1 col2 both populated where col1 is repeated from a previous line when empty.
There are sometimes none, 1, or 2 occurences of an empty col1 but, how else possible, this can also vary :/

Again, any help is welcome!

Iga




 
Code:
awk '2==NF{f1=$1;print;next}{print f1 "\t" $1}' old >new
 
Hi

You did not said anything about separators, so I considered it one or more spaces and ignored the slightly modified aspect of the output.
Code:
awk -F ' +' '{if($1)l=$1;else$1=l;print}' inputfile
[gray]# or[/gray]
sed '/^[^ ]/{h;s/ .*//;x};/^ /{G;s/\(.*\)\n\(.*\)/\2\1/}' inputfile
Note, that the [tt]sed[/tt] solution will probably not work on some Unix system.

Feherke.
 
awk 'NF==2{a=$1}{print a"\t"$NF}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hello all,

this option works:
awk 'NF==2{a=$1}{print a"\t"$NF}' test > output

When I execute it on a file with nice tabs delimited columns.
The file I generate however has many white spaces, and for some reason ex insists on inserting ^M before each line in the file.
How can I quickly remove those strange characters from the file ?
many whitespaces are not a problem for the awk rule above.
Tested that with a file.

Many thanks guys :)

PS, Feherke, are you from .be ?
Join the club ;)
 
Hello All,

I dos2unix'ed the input file and that removes the ^M characters.
Very strange since ex seems responsible for inserting them ?

Anyway, this is really very good help!

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top