Sed Question - File Reformat

Sed Question - File Reformat

Post by Graem » Tue, 04 Dec 2001 14:34:28



Greetings ;

I'm a newbie with scripts and have the following file  ;

-------------------------------------
field1
field2
field3
field4
field5
-------------------------------------
field1
field2
field3
field4
to a maximum of eight fields
etc

I've cut and sedded my way to this stage but I'm pulling out hair here !

I would like to produce a fixed length file off  all fields following
the -------
excluding the line immediatly after the ---------

ie

field1    field2    field3  field4 field5 field6 field7  field8
ditto

 
 
 

Sed Question - File Reformat

Post by Chris F.A. Johnso » Tue, 04 Dec 2001 17:01:32



> Greetings ;

> I'm a newbie with scripts and have the following file  ;

> -------------------------------------
> field1
> field2
> field3
> field4
> field5
> -------------------------------------
> field1
> field2
> field3
> field4
> to a maximum of eight fields
> etc

> I've cut and sedded my way to this stage but I'm pulling out hair here !

> I would like to produce a fixed length file off  all fields following
> the -------
> excluding the line immediatly after the ---------

> ie

> field1    field2    field3  field4 field5 field6 field7  field8
> ditto

What do you mean by a "fixed length file"?

Do you mean a file with lines containing fixed-length records (i.e. all
lines are the same length)?
Or lines comprising fixed-length fields?
Or do mean, literally, a fixed-length file?

And you say "excluding the line immediately after the ---------", but
that's not what your example shows.

This awk script will produce fixed-length lines filled with fixed-length
fields:

awk '
function printline() {
     printf "%-*s\n", fields * width, line
     }

BEGIN {
        width = 8    ## width of each field
        fields = 8   ## number of fields per line
      }

/-------------------------------------/ {
     if ( line ) { printline(); line = "" }
     getline
     }

    { line = line  sprintf("%-*s", width, $0) }


--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Sed Question - File Reformat

Post by Michael Heimin » Tue, 04 Dec 2001 19:10:29



06:34:

Quote:

> Greetings ;

> I'm a newbie with scripts and have the following file  ;

> -------------------------------------
> field1
> field2
> field3
> field4
> field5
> -------------------------------------
> field1
> field2
> field3
> field4
> to a maximum of eight fields
> etc

> I've cut and sedded my way to this stage but I'm pulling out hair
> here !

> I would like to produce a fixed length file off  all fields
> following the -------
> excluding the line immediatly after the ---------

> ie

> field1    field2    field3  field4 field5 field6 field7  field8
> ditto

Not sure what you're trying to do?

Perhaps, you want something like this (using GNU awk):

awk 'BEGIN{RS="[-]*[-]"}{$1=$1;print $0}' infile

Michael Heiming

 
 
 

Sed Question - File Reformat

Post by Tapani Tarvaine » Tue, 04 Dec 2001 19:16:47



> -------------------------------------
> field1
> field2
> field3
> field4
> field5
> -------------------------------------
> field1
> field2
> field3
> field4
> to a maximum of eight fields
> etc

> I've cut and sedded my way to this stage but I'm pulling out hair here !

> I would like to produce a fixed length file off  all fields following
> the -------
> excluding the line immediatly after the ---------

> ie

> field1    field2    field3  field4 field5 field6 field7  field8

That doesn't look like the line immediately following the -------
line had been excluded... and I'm not sure what you mean by
"fixed length file" either - fixed field lengths?

Anyway, I'd vote awk, something like this:

awk '/^---/{printf "\n"; next} {printf "%9s",$0} END{printf "\n"}' file

If there is a separator line at the end of the file you can omit
the END part.

--
Tapani Tarvainen

 
 
 

Sed Question - File Reformat

Post by Mosh » Tue, 04 Dec 2001 22:45:40


Graeme had nothing better to do than to say:

Quote:> -------------------------------------
> field1
> field2
> field3
> field4
> field5
> -------------------------------------
> field1
> field2
> field3
> field4
> I would like to produce a fixed length file off  all fields following
> the -------
> excluding the line immediatly after the ---------
> field1    field2    field3  field4 field5 field6 field7  field8

Well I tried something with sed, but didn't have enough time to get it
working. Can anyone tell me why this doesn't work? :)

sed -n ':top /^-/{d;x; :mid n; /^-/{x;p;b top;}; /^[^-]/{s/$/ /;H;b mid;};}'

Moshe

--
SPAM BLOCK: Remove bra before replying!

--> Enthusiastic Sesquipedalianism Eradication Proponent <--

 
 
 

Sed Question - File Reformat

Post by Ben.Altma » Wed, 05 Dec 2001 00:08:12



Quote:

> Greetings ;

> I'm a newbie with scripts and have the following file  ;

> -------------------------------------
> field1
> field2
> field3
> field4
> field5
> -------------------------------------
> field1
> field2
> field3
> field4
> to a maximum of eight fields
> etc

> I've cut and sedded my way to this stage but I'm pulling out hair here !

> I would like to produce a fixed length file off  all fields following
> the -------
> excluding the line immediatly after the ---------

> ie

> field1    field2    field3  field4 field5 field6 field7  field8
> ditto

Use awk, but here's a sed solution:
    sed -n '/-----/!{s/$/ / ; H;}
            $ {g; s/.$//; s/\n//gp; q;}
            /-----/ { g; s/ $//; s/\n//gp; s/.//g; h; }' file

line1 appends a space to the field and adds it to the patterns space.
line 2 handles the last line of a file in case it isn't a separator which
like line 3 puts the swap space in the pattern space, removes the padded
space and new lines and prints the line.
line 3 also clears the swap space before continuing with the next line.

regards
Ben Altman

 
 
 

Sed Question - File Reformat

Post by Matthew Land » Wed, 05 Dec 2001 03:03:40



> Greetings ;

> I'm a newbie with scripts and have the following file  ;

> -------------------------------------
> field1
> field2
> field3
> field4
> field5
> -------------------------------------
> field1
> field2
> field3
> field4
> to a maximum of eight fields
> etc

> I've cut and sedded my way to this stage but I'm pulling out hair here !

> I would like to produce a fixed length file off  all fields following
> the -------
> excluding the line immediatly after the ---------

> ie

> field1    field2    field3  field4 field5 field6 field7  field8
> ditto

Use awk instead.

awk '{if ($0 ~/^----/) {printf "\n"} else {printf $0" "}} END{print}' file

It is likely that you could use awk to do all the cut and sed portions
you use to get to this point.  Having one awk command do all the work would
greatly improve time and computing.

 - Matt
--
_______________________________________________________________________

  IBM High Speed Interconnect - Fibre Channel I/O Dev/Test/Support
   << Comments, views, and opinions are mine alone, not IBM's. >>