Manipulating text
Hi all,
I'm in desperate need of reformatting an old text file with some 5300 lines of text. I think that awk or sed is the way to go but am getting confused. The original format is like this: Code:
in: 160.11 Code:
@ Thanks, Phil |
Hi.
I personally don't think sed it the way to go here. All you seem to be doing is moving text, not manupilating it much. I haven't used awk much, so I don't have an opinion here. But a small shell or perl script would do the job quite easily. All you have to do i read a few lines at the time, store the lines or tokens in variables, and the write the stored variables to a file. |
Yeah, for re-formatting like that I'd use Perl. These links will help:
http://perldoc.perl.org/ http://www.perlmonks.org/?node=Tutorials |
Thanks so much kenneho and chrism01. I feel like such an idiot now! :)
I managed to nail together a quick parser for this from PHP actually: Code:
<?php |
awk attempt
1.awk
== begin { INSIDE=NO ; } /in:/ { INVALUE = $2 ; INSIDE=YES; } !/in:/&&!/out:/ { if (length(lines) > 0) lines=sprintf("%s\n%s",lines,$0); else lines=$0 ; } /out:/ { OUTVALUE=$2 ; printf("@\n%s\t\t%s\n",INVALUE,OUTVALUE); print lines ; lines = "" ; INSIDE=NO } end { print lines ; } == gawk -f 1.awk < 1.in == in: in: 160.11 See our bus? It's really big. out: 165.02 in: 165.12 -Is it the great big one here? -That's right. out: 171.03 ... out: @ 160.11 165.02 See our bus? It's really big. @ 165.12 171.03 -Is it the great big one here? -That's right. |
Here's a simpler version I think:
Code:
$ cat doit.pl |
If you don't mind preprocessing w/ sed :), this is pretty short & sweet:
Code:
#! /bin/bash EDIT: I spoke too soon -- here is an all sed script that is the shortest of all: Code:
#!/bin/sed -rf Happy to answer any Q's about how it works. (http://www.gnu.org/software/sed/manual/sed.html) |
All times are GMT -5. The time now is 04:54 PM. |