0

I have a program that will create a text file with output which looks like this:

...
AF
Aug 05 16:27:01.310 [notice] Tor 0.3.5.8 running on Linux with Libevent 2.1.8-stable, OpenSSL 1.1.1c, Zlib 1.2.11, Liblzma 5.2.4, and Libzstd 1.3.8.
Aug 05 16:27:01.310 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Aug 05 16:27:01.311 [notice] Read configuration file "/etc/tor/torrc".
Aug 05 16:27:01.314 [notice] Opening Socks listener on 127.0.0.1:9050
Aug 05 16:27:01.314 [notice] Opened Socks listener on 127.0.0.1:9050
Aug 05 16:27:01.000 [notice] Parsing GEOIP IPv4 file /usr/share/tor/geoip.
Aug 05 16:27:01.000 [notice] Parsing GEOIP IPv6 file /usr/share/tor/geoip6.
Aug 05 16:27:01.000 [notice] Bootstrapped 0%: Starting
Aug 05 16:27:01.000 [notice] Starting with guard context "default"
Aug 05 16:27:11.000 [notice] Catching signal TERM, exiting cleanly.

AX
Aug 05 16:27:11.333 [notice] Tor 0.3.5.8 running on Linux with Libevent 2.1.8-stable, OpenSSL 1.1.1c, Zlib 1.2.11, Liblzma 5.2.4, and Libzstd 1.3.8.
Aug 05 16:27:11.333 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Aug 05 16:27:11.333 [notice] Read configuration file "/etc/tor/torrc".
Aug 05 16:27:11.338 [notice] Opening Socks listener on 127.0.0.1:9050
Aug 05 16:27:11.338 [notice] Opened Socks listener on 127.0.0.1:9050
Aug 05 16:27:11.000 [notice] Parsing GEOIP IPv4 file /usr/share/tor/geoip.
Aug 05 16:27:11.000 [notice] Parsing GEOIP IPv6 file /usr/share/tor/geoip6.
Aug 05 16:27:11.000 [notice] Bootstrapped 0%: Starting
Aug 05 16:27:12.000 [notice] Starting with guard context "default"
Aug 05 16:27:21.000 [notice] Catching signal TERM, exiting cleanly.
...

Essentially, it'll check whether TOR can connect via different countries. I made it so it outputs the ISO alpha-2 code before the paragraph, and then an empty return line.

How can I create a program which can parse each individual paragraph, search for the text '100%', and if it exists, copy the alpha-2 code (above the paragraph) into a different file, using bash '>>'

1

If your input consists of records that are separated by one or more blank lines, then you can use awk or perl in paragraph mode

awk -vRS= '/100%/ {print $1}' file >> newfile

or

perl -00anE 'say $F[0] if /100%/' file >> newfile
  • Thanks a lot, awk was exactly what i needed! If you have a minute, would you mind explaining in a couple words what the vRS options are, and $1, does that represent the 2nd entry in each paragraph (the country code)? – Nick Bailuc Aug 6 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.