[Solved with Python] Perl extract word string

Need help with C, C++, perl, python, etc?

[Solved with Python] Perl extract word string

Postby sunrat » 2018-05-16 12:41

I need to extract only strings with capitalized words from a text file export. Text file is like this:
Code: Select all
{"maps":{"idOthers":{"label":"Others","state":1,"bkmrk":{"id1517321708544042":{"label":"Thornbury","latlng":"-37.758959,145.002408","z":15},"id1517404948177044":{"label":"SE Oz","latlng":"-37.383253,146.799316","z":7},"id1524907539056020":{"label":"Melbourne wide","latlng":"-37.736512,145.266724","z":10},"id1526128108894092":{"label":"Melbourne city","latlng":"-37.814412,144.963698","z":15}}}},"portals":{"idOthers":{"label":"Others","state":1,"bkmrk":{"id1519870156505020":{"guid":"bd3c83881e864833a1d6ba7f74d48932.16","latlng":"-28.101867,140.198912","label":"Moomba Airport"},"id1524017293988041":{"guid":"5d28dfb8e5c74235bcb6ebabe7db137e.16","latlng":"-38.052737,140.699065","label":"MacDonnell Water Tower"},"id1524017431668144":{"guid":"cef5a3a6052b4db5918f240633c87d1a.16","latlng":"-35.354197,145.725536","label":"Horgan Walk"},"id1524017542894278":{"guid":"981d883b207a4f469d1cfd6ef6a83c99.16","latlng":"-35.651077,145.572401","label":"Finley and District Museum"},"id1524017595214323":{"guid":"9c02931bd84a44f09d0eb7d528440dde.16","latlng":"-40.121934,148.016741","label":"Flinders Anchor"},"id1524018796438428":{"guid":"729ba75fe8af4915ad0171398308dd1a.16","latlng":"-34.276904,146.053927","label":"Griffith Water Tank 3"},"id1524018933096574":{"guid":"f9f356e2a6cb4311b8d5e6b8597e9f10.16","latlng":"-38.379673,141.369564","label":"The Blowholes"},"id1524573178960029":{"guid":"4807ea2a1f5446eca437f30d00249070.16","latlng":"-35.812108,145.564549","label":"Murraay Valley Trail"},"id1524573424316121":{"guid":"da1fa66c47e04ab8bd63ca889477fc64.16","latlng":"-35.919043,145.64944","label":"Cobram Court House"},"id1525240907654076":{"guid":"f7f0413875e849f0bd4144deeb2e1dd3.16","latlng":"-37.10952,142.412065","label":"Keep Wildlife Wild"},"id1524573612772224":{"guid":"ee648daae1954dfaae922dbf8b73b628.16","latlng":"-35.355555,145.725977","label":"Jerilderie Library"}}}}}


I tried these 2 perl commands found on stackexchange but they extract each word to a separate line. Is there a way to extract so each string is on a line, no matter if it is 1,2,3 or whatever number of words? File is called "bookmarks" and they are location markers for Ingress portals. I tried to modify but my perl-fu is weak.

Code: Select all
perl -e 'print "$_\n" for map {m/(\w*[A-Z]+\w*)/g} <>' bookmarks
or
perl -nE 'say $1  while /(\w*[A-Z]+\w*)/g' bookmarks
Last edited by sunrat on 2018-06-13 12:36, edited 1 time in total.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2356
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Perl extract word string

Postby arzgi » 2018-05-21 16:02

sunrat wrote:I need to extract only strings with capitalized words from a text file export. Text file is like this:

I tried these 2 perl commands found on stackexchange but they extract each word to a separate line. Is there a way to extract so each string is on a line, no matter if it is 1,2,3 or whatever number of words? File is called "bookmarks" and they are location markers for Ingress portals. I tried to modify but my perl-fu is weak.

Code: Select all
perl -e 'print "$_\n" for map {m/(\w*[A-Z]+\w*)/g} <>' bookmarks
or
perl -nE 'say $1  while /(\w*[A-Z]+\w*)/g' bookmarks


My knowledge of perl is even weaker than yours, but if python solution is allowed, here one possible.
File opening is superfluos, but it took a while to notice your file is one line. Or let's say this is general approach, script accepts files that have one or more (>=1) lines,
Code: Select all
#! /usr/bin/env python3

file = open("bookmarks", "r")
while True:
    line = file.readline()
    if len(line) == 0:
        break
    words = line.split('"')
    for word in words:
        if word[0].isupper():
            print(word, end="\n")
file.close()


Reguires python3.
Make executable:
Code: Select all
chmod 700 split.py

Then run in same directory, where bookmarks are:
Code: Select all
./split.py


Output:
Code: Select all
Others
Thornbury
SE Oz
Melbourne wide
Melbourne city
...
arzgi
 
Posts: 182
Joined: 2008-02-21 17:03
Location: Finland

Re: Perl extract word string

Postby sunrat » 2018-06-13 00:17

Just to complete this, arzgi's script didn't work after copying and pasting from here. It took a while to work out, after a few PMs, that the spaces were not copied correctly. After replacing the spaces with tabs in a text editor, it worked perfectly. I'm advised that replacing the spaces with spaces should have worked instead.
So be careful copying Python scripts, they may have fake spaces when copied from HTML pages!
Many thanks to arzgi for his help with this. 8)
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2356
Joined: 2006-08-29 09:12
Location: Melbourne, Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

fashionable