The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Help with an awk/sed statement.

azith28azith28 Registered User regular

Hi,

Trying to figure out how to use awk or sed for this.

Say i have a long input file list.txt with identical formatting like so:

===========
TYPE:DATA1
SEQ-NBR:1
DESCR: Stuff goes here
char-fld[1]
char-fld[2]
char-fld[3]
char-fld[4]
char-fld[5]
===========
TYPE:DATA2
SEQ-NBR:1
DESCR:Morestuff
char-fld[1]
char-fld[2]
char-fld[3]
char-fld[4]
char-fld[5]
==========
etc, and so on and so forth.

What id like to do is take this input file, and slice it into many seperate files named whatever is in the "TYPE" field so it would become:
DATA1.txt
DATA2.txt
etc.

However it would also have to look at the SEQ-NBR field since there could be more then one of the same TYPE with different sequence numbers...so it would end up looking more like.
DATA1.1.txt
DATA2.1.txt
with potentially:
DATA1.2.txt
as well.

with each file looking like this after the slices:

TYPE:DATA1
SEQ-NBR:1
DESCR: Stuff goes here
char-fld[1]
char-fld[2]
char-fld[3]
char-fld[4]
char-fld[5]

Thanks bearded admins.

Stercus, Stercus, Stercus, Morituri Sum

Posts

  • Pure DinPure Din Boston-areaRegistered User regular
    Is each file exactly the same number of lines?

    As a beardless software engineer, my approach would probably be, use 'split -l 9' (or whatever) to break the file up, tail to get rid of the line on top of each file, then just write a script with a loop to do the renaming.

    It's not as pretty as an awk one-liner but could be a good deal faster if we're talking about a massive amount of data.

  • azith28azith28 Registered User regular
    im starting with a large file but yes the number of lines should remain static that i want to capture.

    Stercus, Stercus, Stercus, Morituri Sum
Sign In or Register to comment.