Godzilla74 Godzilla74 - 6 months ago 7
Ruby Question

Parse TXT with Ruby and print certain fields

I have a pretty massive txt file that I'm parsing and I want to only extract certain values from a line if it matches what I'm looking for. Right now, my criteria is just "US", which is always in the 3rd column of my file.

Here is a sample of the data:

1 GARANTOVANA 01|GRT1|XE|6077382046794|EQ0000000004928892|Common Stock|Equity|BBG000RXQ320|BBG000RXQ0H0|32645|||0|39666|//blp/mktdata/bsym/XE/GRT1|//blp/mktdata/buid/XE/EQ0000000004928892|//blp/mktdata/bsid/6077382046794
1 GARANTOVANA 01|GRT1|EU|6210525644566|EQ0000000004928896|Common Stock|Equity|BBG000RXQ6Z7|BBG000RXQ6Q7|44954|||0|44955|//blp/mktdata/bsym/EU/GRT1|//blp/mktdata/buid/EU/EQ0000000004928896|//blp/mktdata/bsid/6210525644566
1 GARANTOVANA 01|GRT1|XW|16643001594954|EQ0000000004928892|Common Stock|Equity|BBG000RXQ6J5|BBG000RXQ0H0|14039|||0|39503|//blp/mktdata/bsym/XW/GRT1|//blp/mktdata/buid/XW/EQ0000000004928892|//blp/mktdata/bsid/16643001594954
1 GARANTOVANA 01|GRT1|SK|6532651738965|EQ0000000000499454|Common Stock|Equity|BBG000PPZ613|BBG000PPZ5P9|27262|27262||0|39599|//blp/mktdata/bsym/SK/GRT1|//blp/mktdata/buid/SK/EQ0000000000499454|//blp/mktdata/bsid/6532651738965
1 GARANTOVANA 01|1GRT1T|B3|27118466846909|EQ0000000036533736|Common Stock|Equity|BBG006T7TR80|BBG006T7TR71|35535|||0|39704|//blp/mktdata/bsym/B3/1GRT1T|//blp/mktdata/buid/B3/EQ0000000036533736|//blp/mktdata/bsid/27118466846909
1 GARANTOVANA 01|1GRT1T|L3|27127056807711|EQ0000000036634423|Common Stock|Equity|BBG006TC4Y09|BBG006TC4XZ3|35535|||0|39704|//blp/mktdata/bsym/L3/1GRT1T|//blp/mktdata/buid/L3/EQ0000000036634423|//blp/mktdata/bsid/27127056807711
1 GARANTOVANA 01|1GRT1T|S2|27114171916930|EQ0000000036685427|Common Stock|Equity|BBG006TCWR94|BBG006TCWR85|35535|||0|39704|//blp/mktdata/bsym/S2/1GRT1T|//blp/mktdata/buid/S2/EQ0000000036685427|//blp/mktdata/bsid/27114171916930
1 GARANTOVANA 01|1GRT1T|PO|29102748938392|EQ0000000042300111|Common Stock|Equity|BBG008GZ4M77|BBG008GZ4M68|35535|||0|39704|//blp/mktdata/bsym/PO/1GRT1T|//blp/mktdata/buid/PO/EQ0000000042300111|//blp/mktdata/bsid/29102748938392
1 GARANTOVANA 02|GRT2|XA|6047317275723|EQ0000000004928900|Common Stock|Equity|BBG000RXQFH7|BBG000RXQ7C0|32774|||0|39669|//blp/mktdata/bsym/XA/GRT2|//blp/mktdata/buid/XA/EQ0000000004928900|//blp/mktdata/bsid/6047317275723
1 GARANTOVANA 02|GRT2|XG|6528354244230|EQ0000000004928900|Common Stock|Equity|BBG000RXQCN7|BBG000RXQ7C0|46707|||0||//blp/mktdata/bsym/XG/GRT2|//blp/mktdata/buid/XG/EQ0000000004928900|//blp/mktdata/bsid/6528354244230
1 GARANTOVANA 02|GRT2|XN|6098856883275|EQ0000000004928900|Common Stock|Equity|BBG000RXQD82|BBG000RXQ7C0|32820|||0|39677|//blp/mktdata/bsym/XN/GRT2|//blp/mktdata/buid/XN/EQ0000000004928900|//blp/mktdata/bsid/6098856883275
1 GARANTOVANA 02|GRT2|XV|26117699449931|EQ0000000004928900|Common Stock|Equity|BBG00591KH88|BBG000RXQ7C0|57346|||0|57347|//blp/mktdata/bsym/XV/GRT2|//blp/mktdata/buid/XV/EQ0000000004928900|//blp/mktdata/bsid/26117699449931
1 GARANTOVANA 02|GRT2|XB|6051612243019|EQ0000000004928900|Common Stock|Equity|BBG000RXQ7P6|BBG000RXQ7C0|37355|37355||0|39723|//blp/mktdata/bsym/XB/GRT2|//blp/mktdata/buid/XB/EQ0000000004928900|//blp/mktdata/bsid/6051612243019
1 GARANTOVANA 02|GRT2|XD|6073087079499|EQ0000000004928900|Common Stock|Equity|BBG000RXQ9W4|BBG000RXQ7C0|32780|||0|39670|//blp/mktdata/bsym/XD/GRT2|//blp/mktdata/buid/XD/EQ0000000004928900|//blp/mktdata/bsid/6073087079499
1 GARANTOVANA 02|GRT2|XE|6077382046795|EQ0000000004928900|Common Stock|Equity|BBG000RXQBQ6|BBG000RXQ7C0|32645|||0|39666|//blp/mktdata/bsym/XE/GRT2|//blp/mktdata/buid/XE/EQ0000000004928900|//blp/mktdata/bsid/6077382046795
1 GARANTOVANA 02|GRT2|XC|6064497144907|EQ0000000004928900|Common Stock|Equity|BBG000RXQ8C8|BBG000RXQ7C0|35535|||0|0|//blp/mktdata/bsym/XC/GRT2|//blp/mktdata/buid/XC/EQ0000000004928900|//blp/mktdata/bsid/6064497144907
1 GARANTOVANA 02|GRT2|XH|6060202177611|EQ0000000004928900|Common Stock|Equity|BBG000RXQ829|BBG000RXQ7C0|32800|||0|39674|//blp/mktdata/bsym/XH/GRT2|//blp/mktdata/buid/XH/EQ0000000004928900|//blp/mktdata/bsid/6060202177611
1 GARANTOVANA 02|GRT2|XI|6055907210315|EQ0000000004928900|Common Stock|Equity|BBG000RXQ7T2|BBG000RXQ7C0|32795|||0|39671|//blp/mktdata/bsym/XI/GRT2|//blp/mktdata/buid/XI/EQ0000000004928900|//blp/mktdata/bsid/6055907210315
1 GARANTOVANA 02|GRT2|XJ|6111741785163|EQ0000000004928900|Common Stock|Equity|BBG000RXQC20|BBG000RXQ7C0|32774|||0|39669|//blp/mktdata/bsym/XJ/GRT2|//blp/mktdata/buid/XJ/EQ0000000004928900|//blp/mktdata/bsid/6111741785163
1 GARANTOVANA 02|GRT2|XF|6068792112203|EQ0000000004928900|Common Stock|Equity|BBG000RXQ9R0|BBG000RXQ7C0|14091|||0|39534|//blp/mktdata/bsym/XF/GRT2|//blp/mktdata/buid/XF/EQ0000000004928900|//blp/mktdata/bsid/6068792112203
1 GARANTOVANA 02|GRT2|XO|6094561915979|EQ0000000004928900|Common Stock|Equity|BBG000RXQCX6|BBG000RXQ7C0|32796|||0|39672|//blp/mktdata/bsym/XO/GRT2|//blp/mktdata/buid/XO/EQ0000000004928900|//blp/mktdata/bsid/6094561915979
1 GARANTOVANA 02|GRT2|XP|6103151850571|EQ0000000004928900|Common Stock|Equity|BBG000RXQDR1|BBG000RXQ7C0|28673|||0|39627|//blp/mktdata/bsym/XP/GRT2|//blp/mktdata/buid/XP/EQ0000000004928900|//blp/mktdata/bsid/6103151850571
1 GARANTOVANA 02|GRT2|XS|6107446817867|EQ0000000004928900|Common Stock|Equity|BBG000RXQFD1|BBG000RXQ7C0|32802|||0|39676|//blp/mktdata/bsym/XS/GRT2|//blp/mktdata/buid/XS/EQ0000000004928900|//blp/mktdata/bsid/6107446817867
1 GARANTOVANA 02|GRT2|XT|10118946272331|EQ0000000004928900|Common Stock|Equity|BBG000RXQGH5|BBG000RXQ7C0|36311|||0|0|//blp/mktdata/bsym/XT/GRT2|//blp/mktdata/buid/XT/EQ0000000004928900|//blp/mktdata/bsid/10118946272331
1 GARANTOVANA 02|GRT2|XW|16643001594955|EQ0000000004928900|Common Stock|Equity|BBG000RXQGM9|BBG000RXQ7C0|14039|||0|39503|//blp/mktdata/bsym/XW/GRT2|//blp/mktdata/buid/XW/EQ0000000004928900|//blp/mktdata/bsid/16643001594955
1 GARANTOVANA 02|GRT2|EU|6210525644568|EQ0000000004928904|Common Stock|Equity|BBG000RXQGY6|BBG000RXQGV9|44954|||0|44955|//blp/mktdata/bsym/EU/GRT2|//blp/mktdata/buid/EU/EQ0000000004928904|//blp/mktdata/bsid/6210525644568
1 GARANTOVANA 02|GRT2|XL|6081677014091|EQ0000000004928900|Common Stock|Equity|BBG000RXQCG5|BBG000RXQ7C0|32733|||0|39667|//blp/mktdata/bsym/XL/GRT2|//blp/mktdata/buid/XL/EQ0000000004928900|//blp/mktdata/bsid/6081677014091
1 GARANTOVANA 02|GRT2|SK|6532651738964|EQ0000000000488919|Common Stock|Equity|BBG000PPCX91|BBG000PPCX19|27262|27262||0|39599|//blp/mktdata/bsym/SK/GRT2|//blp/mktdata/buid/SK/EQ0000000000488919|//blp/mktdata/bsid/6532651738964
1 GARANTOVANA 02|1GRT2T|B3|27118466847035|EQ0000000036533742|Common Stock|Equity|BBG006T7TRB6|BBG006T7TR99|35535|||0|39704|//blp/mktdata/bsym/B3/1GRT2T|//blp/mktdata/buid/B3/EQ0000000036533742|//blp/mktdata/bsid/27118466847035
1 GARANTOVANA 02|1GRT2T|L3|27127056807696|EQ0000000036634429|Common Stock|Equity|BBG006TC4Y27|BBG006TC4Y18|35535|||0|39704|//blp/mktdata/bsym/L3/1GRT2T|//blp/mktdata/buid/L3/EQ0000000036634429|//blp/mktdata/bsid/27127056807696
1 GARANTOVANA 02|1GRT2T|S2|27114171915766|EQ0000000036685433|Common Stock|Equity|BBG006TCWRC0|BBG006TCWRB1|35535|||0|39704|//blp/mktdata/bsym/S2/1GRT2T|//blp/mktdata/buid/S2/EQ0000000036685433|//blp/mktdata/bsid/27114171915766
1 GARANTOVANA 02|1GRT2T|PO|29102748938476|EQ0000000042300117|Common Stock|Equity|BBG008GZ4M95|BBG008GZ4M86|35535|||0|39704|//blp/mktdata/bsym/PO/1GRT2T|//blp/mktdata/buid/PO/EQ0000000042300117|//blp/mktdata/bsid/29102748938476
1 MAJ-DRVODJELSKA AD BANJA L|MJDRRA|BK|7395940166279|EQ0000000006233051|Common Stock|Equity|BBG000V36LZ7|BBG000V36LT4|31827|37445||0|39661|//blp/mktdata/bsym/BK/MJDRRA|//blp/mktdata/buid/BK/EQ0000000006233051|//blp/mktdata/bsid/7395940166279
1 MAJ-ZAVRSNI RADOVI U GRAD|MJZRRA|BK|7395940166280|EQ0000000006233047|Common Stock|Equity|BBG000V36MJ3|BBG000V36M30|31827|37445||0|39661|//blp/mktdata/bsym/BK/MJZRRA|//blp/mktdata/buid/BK/EQ0000000006233047|//blp/mktdata/bsid/7395940166280
1 PRODUCTION FILM CO|8458|TT|751664892539|EQ0000000038275929|Common Stock|Equity|BBG007BVVLP5|BBG007BVVLL9|14132|14132||0|39557|//blp/mktdata/bsym/TT/8458|//blp/mktdata/buid/TT/EQ0000000038275929|//blp/mktdata/bsid/751664892539
1-800-ATTORNEY INC|ATTY|US|399432585156|EQ0018607400001000|Common Stock|Equity|BBG000NX0XB6|BBG000NX0XB6|14228|14170||0|40182|//blp/mktdata/bsym/US/ATTY|//blp/mktdata/buid/US/EQ0018607400001000|//blp/mktdata/bsid/399432585156
1-800-ATTORNEY INC|ATTY|UV|1584843558852|EQ0018607400001000|Common Stock|Equity|BBG000NX28J1|BBG000NX0XB6|14005|||0|39493|//blp/mktdata/bsym/UV/ATTY|//blp/mktdata/buid/UV/EQ0018607400001000|//blp/mktdata/bsid/1584843558852
1-800-FLOWERS.COM INC-CL A|FLWS|UP|554051298597|EQ0090040500001000|Common Stock|Equity|BBG000BBRDB2|BBG000BBR9P6|14005|29366||0|39493|//blp/mktdata/bsym/UP/FLWS|//blp/mktdata/buid/UP/EQ0090040500001000|//blp/mktdata/bsid/554051298597
1-800-FLOWERS.COM INC-CL A|FLWS|VY|15010911216933|EQ0090040500001000|Common Stock|Equity|BBG000BBRP25|BBG000BBR9P6|14005|||0|39493|//blp/mktdata/bsym/VY/FLWS|//blp/mktdata/buid/VY/EQ0090040500001000|//blp/mktdata/bsid/15010911216933
1-800-FLOWERS.COM INC-CL A|FLWS|US|399432475941|EQ0090040500001000|Common Stock|Equity|BBG000BBR9P6|BBG000BBR9P6|14005|14023||0|39493|//blp/mktdata/bsym/US/FLWS|//blp/mktdata/buid/US/EQ0090040500001000|//blp/mktdata/bsid/399432475941
1-800-FLOWERS.COM INC-CL A|FLWS|UC|579821102373|EQ0090040500001000|Common Stock|Equity|BBG000BBRBN3|BBG000BBR9P6|14005|36284||0|39493|//blp/mktdata/bsym/UC/FLWS|//blp/mktdata/buid/UC/EQ0090040500001000|//blp/mktdata/bsid/579821102373
1-800-FLOWERS.COM INC-CL A|FLWS|UB|639950644517|EQ0090040500001000|Common Stock|Equity|BBG000BBRFD5|BBG000BBR9P6|14005|||0|39493|//blp/mktdata/bsym/UB/FLWS|//blp/mktdata/buid/UB/EQ0090040500001000|//blp/mktdata/bsid/639950644517
1-800-FLOWERS.COM INC-CL A|FLWS|UM|687195284773|EQ0090040500001000|Common Stock|Equity|BBG000BBRG43|BBG000BBR9P6|14005|||0|39493|//blp/mktdata/bsym/UM/FLWS|//blp/mktdata/buid/UM/EQ0090040500001000|//blp/mktdata/bsid/687195284773
1-800-FLOWERS.COM INC-CL A|FLWS|UX|541166396709|EQ0090040500001000|Common Stock|Equity|BBG000BBRGQ9|BBG000BBR9P6|14005|||0|39493|//blp/mktdata/bsym/UX/FLWS|//blp/mktdata/buid/UX/EQ0090040500001000|//blp/mktdata/bsid/541166396709
1-800-FLOWERS.COM INC-CL A|FLWS|UD|1490354169125|EQ0090040500001000|Common Stock|Equity|BBG000BBRM68|BBG000BBR9P6|14005|||0|39493|//blp/mktdata/bsym/UD/FLWS|//blp/mktdata/buid/UD/EQ0090040500001000|//blp/mktdata/bsid/1490354169125
1-800-FLOWERS.COM INC-CL A|FLWS|UW|635655677221|EQ0090040500001000|Common Stock|Equity|BBG000BBRN66|BBG000BBR9P6|14005||14080|0|39493|//blp/mktdata/bsym/UW/FLWS|//blp/mktdata/buid/UW/EQ0090040500001000|//blp/mktdata/bsid/635655677221
1-800-FLOWERS.COM INC-CL A|FLWS|UF|8293582365989|EQ0090040500001000|Common Stock|Equity|BBG000BBRNN7|BBG000BBR9P6|14005|38552||0|39493|//blp/mktdata/bsym/UF/FLWS|//blp/mktdata/buid/UF/EQ0090040500001000|//blp/mktdata/bsid/8293582365989
1-800-FLOWERS.COM INC-CL A|FLWS|VJ|15015206184229|EQ0090040500001000|Common Stock|Equity|BBG000BBRPH9|BBG000BBR9P6|14005|57923||0|39493|//blp/mktdata/bsym/VJ/FLWS|//blp/mktdata/buid/VJ/EQ0090040500001000|//blp/mktdata/bsid/15015206184229
1-800-FLOWERS.COM INC-CL A|FLWS|VK|15019501151525|EQ0090040500001000|Common Stock|Equity|BBG000BBRPS7|BBG000BBR9P6|14005|57925||0|39493|//blp/mktdata/bsym/VK/FLWS|//blp/mktdata/buid/VK/EQ0090040500001000|//blp/mktdata/bsid/15019501151525
1-800-FLOWERS.COM INC-CL A|FWC|GF|433792279065|EQ0090040500001001|Common Stock|Equity|BBG000C0S2W5|BBG000C0S2P3|38421|||0|39735|//blp/mktdata/bsym/GF/FWC|//blp/mktdata/buid/GF/EQ0090040500001001|//blp/mktdata/bsid/433792279065
1-800-FLOWERS.COM INC-CL A|FWC|GR|395137573401|EQ0090040500001001|Common Stock|Equity|BBG000C0S2P3|BBG000C0S2P3|14230|||0|40178|//blp/mktdata/bsym/GR/FWC|//blp/mktdata/buid/GR/EQ0090040500001001|//blp/mktdata/bsid/395137573401
1-800-FLOWERS.COM INC-CL A|FWC|GM|1649268023833|EQ0090040500001001|Common Stock|Equity|BBG000C0S4K4|BBG000C0S2P3|14193|||0|39583|//blp/mktdata/bsym/GM/FWC|//blp/mktdata/buid/GM/EQ0090040500001001|//blp/mktdata/bsid/1649268023833
1-800-FLOWERS.COM INC-CL A|FWC|GB|1623498220057|EQ0090040500001001|Common Stock|Equity|BBG000C0S4T5|BBG000C0S2P3|14193|||0|39583|//blp/mktdata/bsym/GB/FWC|//blp/mktdata/buid/GB/EQ0090040500001001|//blp/mktdata/bsid/1623498220057
1-800-FLOWERS.COM INC-CL A|FWC|GS|1675037827609|EQ0090040500001001|Common Stock|Equity|BBG000C0S3L5|BBG000C0S2P3|14194|||0|39584|//blp/mktdata/bsym/GS/FWC|//blp/mktdata/buid/GS/EQ0090040500001001|//blp/mktdata/bsid/1675037827609


And here is my ruby script:

Dir["#{folder}/*"].each do |file|
next if File.directory? file
File.open(file).each do |line|
if line.split(/\s*\|\s*/).include? "US"
puts line
end
end
end


This goes through the file fine, however if there is a "US" anywhere in the line, it will print that line, even if the 3rd field is something else (not US).

Furthermore, when I want to print what would be field 1 (the stock name) & field 2 (the stock symbol) I can't simply use
puts line[0]
since that is just getting the first letter.

Have I gone about creating this parser incorrectly? Is there a better way to do the matching that I'm attempting that would allow me to print the 1st and 3rd fields in the file?

Answer
Dir["#{folder}/*"].each do |file|
  next if File.directory? file
    File.open(file).each do |line|
      parsed_line = line.split('|')
        if parsed_line[2] == "US"
          puts parsed_line[0] + parsed_line[1]            
        end
     end
 end