Parse fixed-width files

后端 未结 3 1086
南方客
南方客 2021-01-12 00:09

I have a lot of text files with fixed-width fields:

            
Dave    Thomas    123 Main
Dan     Anderson  456 Center
Wilma   R         


        
3条回答
  •  自闭症患者
    2021-01-12 00:47

    Just use Perl's unpack function. Something like this:

    while () {
        my ($first,$last,$street) = unpack("A9A25A50",$_);
    
        
    }
    

    Inside the unpack template, the "A###", you can put the width of the field for each A. There are a variety of other formats that you can use to mix and match with, that is, integer fields, etc... If the file is fixed width, like mainframe files, then this should be the easiest.

提交回复
热议问题