Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Manipulate a fixed length file using sed/awk

  • 29-08-2005 2:25pm
    #1
    Closed Accounts Posts: 3,357 ✭✭✭


    I have a fixed length file that I want to reformat.

    Each record consists of 25 chars of 'header' info, then either 1 or 2 subrecords of length 260. So each record looks like this
    <..25..><...260...><...260...>\n or
    <..25..><...260...>\n

    At the moment I have this code to take split each 2 subrecord record into 2 records.
    eg
    <..25..><...subrec1...><...subrec2..>\n

    becomes

    <..25..><...subrec1..>\n
    <..25..><...subrec2..>\n

    This is my prototype code
    #create single leg file
    cut -c 1-285 ${mqfile} > mq1.txt
    #create 2nd leg file
    cut -c 1-25,286-546 ${mqfile} > mq2.txt
    #cat files
    cat mq1.txt mq2.txt > mq1.txt
    

    It works but Im sure there is a better way to this with sed and/or awk which will preferably let me maintain the original record order.

    I need a hint in the right direction! Oh and perl etc isnt available to me.


Comments

  • Closed Accounts Posts: 96 ✭✭krinDar


    I have a fixed length file that I want to reformat.

    One method in awk is using the substr function.

    if you cat this into a file:
    {
           header=substr($0,0,26);
           rec_one=substr($0,26,260);
           rec_two=substr($0,285,260);
    
          if ( length(rec_one) > 5 ) 
          {
                print header rec_one     
          }
    
         if ( length(rec_two) > 5 )
         {
                 print header rec_two 
         }
    
    }
    
    

    and run it as follows:
    awk -f file.awk record_file.txt

    This is pretty kludgey, but it does the job and should be straightforward to use. The strength of awk is that the breaks lines up based on seperators, such as commas, but it is not so handy with fixed length records.

    You may need to change some of the values it uses in substr to suit your example.


  • Closed Accounts Posts: 3,357 ✭✭✭secret_squirrel


    That worked a treat - many thanks. Seems more efficient too. :)


Advertisement