-1

I have a file called adrese.csv that contains the first lines from another file called rezultate2.txt. For example:

www.afahc.ro/ro/rcic/simpozion/Simpozion_13/first_call.html    
www.anmb.ro/ro/files/bric/mars_bric_2008/corespondente.html
www.anmb.ro/ro/files/bric/mars_bric_2011/corespondente.html
www.anmb.ro/ro/files/bric/mars_bric_2015/corespondente.html
www.anmb.ro/ro/files/bric/mars_bric_2017/corespondente.html
www.anmb.ro/ro/files/erasmus/parteneriate.html   
www.anmb.ro/ro/files/studenti/ccoc/1/obiective.html
www.anmb.ro/ro/files/studenti/ccoc/2/servicii.html
www.anmb.ro/ro/files/studenti/ccoc/3/aspecte.html

The file rezultate2.txt contains beside these first lines a batch of text that has the word ”abandon” in it. I need to run grep -E -o ".{0,50}abandon.{0,50}" on rezultate2.txt for each line starting with the string in adrese.csv and output everything to a file, preferably a CSV. I tried different commands but nothing worked.

Sample rezultate2.txt

2345678abcd www.utm.ro/proiecte-europene/despre-pos-dru/index.html-
www.utm.ro/proiecte-europene/despre-pos-dru/index.html:Axa prioritară 2 finanţează activităţi care urmăresc facilitarea tranziţiei de la şcoală la viaţa activă prin dezvoltarea de programe integrate de orientare şi consiliere în carieră şi prin sprijinirea parteneriatelor între şcoli, universităţi şi întreprinderi; prevenirea şi corectarea fenomenului de părăsire timpurie a şcolii prin programe integrate pentru prevenirea abandonului şcolar, încurajarea participării şcolare şi reintegrarea celor care au părăsit şcoala timpuriu; creşterea accesului şi participării la formare profesională continuă prin diversificarea programelor de formare profesională continuă şi sprijinirea participării angajaţilor la astfel de programe.

www.utm.ro/proiecte-europene/despre-pos-dru/index.html-

Axa Prioritară 3 “Creşterea adaptabilităţii lucrătorilor şi a întreprinderilor”

12345678abcd www.utm.ro/posdru141699.1-
www.utm.ro/posdru141699.1: Axa prioritară 2 finanţează activităţi care urmăresc facilitarea tranziţiei de la şcoală la viaţa activă prin dezvoltarea de programe integrate de orientare şi consiliere în carieră şi prin sprijinirea parteneriatelor între şcoli, universităţi şi întreprinderi, prevenirea şi corectarea fenomenului de părăsire timpurie a şcolii prin programe integrate pentru prevenirea abandonului şcolar, încurajarea participării şcolare şi reintegrarea celor care au părăsit şcoala timpuriu, creşterea accesului şi participării la formare profesională continuă prin diversificarea programelor de formare profesională continuă şi sprijinirea participării angajaţilor la astfel de programe. www.utm.ro/posdru141699.1- 12345678abcd www.utm.ro/posdru141699/index.html-
www.utm.ro/posdru141699/index.html: Axa prioritară 2 finanţează activităţi care urmăresc facilitarea tranziţiei de la şcoală la viaţa activă prin dezvoltarea de programe integrate de orientare şi consiliere în carieră şi prin sprijinirea parteneriatelor între şcoli, universităţi şi întreprinderi, prevenirea şi corectarea fenomenului de părăsire timpurie a şcolii prin programe integrate pentru prevenirea abandonului şcolar, încurajarea participării şcolare şi reintegrarea celor care au părăsit şcoala timpuriu, creşterea accesului şi participării la formare profesională continuă prin diversificarea programelor de formare profesională continuă şi sprijinirea participării angajaţilor la astfel de programe. www.utm.ro/posdru141699/index.html- 12345678abcd www.utm.ro/posdru141699/Despre_POSDRU.html-
www.utm.ro/posdru141699/Despre_POSDRU.html: Axa prioritară 2 finanţează activităţi care urmăresc facilitarea tranziţiei de la şcoală la viaţa activă prin dezvoltarea de programe integrate de orientare şi consiliere în carieră şi prin sprijinirea parteneriatelor între şcoli, universităţi şi întreprinderi, prevenirea şi corectarea fenomenului de părăsire timpurie a şcolii prin programe integrate pentru prevenirea abandonului şcolar, încurajarea participării şcolare şi reintegrarea celor care au părăsit şcoala timpuriu, creşterea accesului şi participării la formare profesională continuă prin diversificarea programelor de formare profesională continuă şi sprijinirea participării angajaţilor la astfel de programe. www.utm.ro/posdru141699/Despre_POSDRU.html- 12345678abcd www.utm.ro/en/proiecte-europene/despre-pos-dru/index.html- www.utm.ro/en/proiecte-europene/despre-pos-dru/index.html:Axa prioritară 2 finanţează activităţi care urmăresc facilitarea tranziţiei de la şcoală la viaţa activă prin dezvoltarea de programe integrate de orientare şi consiliere în carieră şi prin sprijinirea parteneriatelor între şcoli, universităţi şi întreprinderi; prevenirea şi corectarea fenomenului de părăsire timpurie a şcolii prin programe integrate pentru prevenirea abandonului şcolar, încurajarea participării şcolare şi reintegrarea celor care au părăsit şcoala timpuriu; creşterea accesului şi participării la formare profesională continuă prin diversificarea programelor de formare profesională continuă şi sprijinirea participării angajaţilor la astfel de programe. www.utm.ro/en/proiecte-europene/despre-pos-dru/index.html-

Axa Prioritară 3 “Creşterea adaptabilităţii lucrătorilor şi a întreprinderilor”

Desired output is a file containing the line in adrese.csv and it's corresponding text with just the word "abandon" and the 50 chars to the left and right of it:

www.utgjiu.ro/despre/index.html?p=5400 în timp ce vechi așezări se depopulează și cad în abandon. Nevoia unor sisteme de referință este, poate, ma


Managed to get what I needed with the help of a friend. The Python script below was written by Radu Eosif Mihăilescu:

#!/usr/bin/python

"""Custom work for Razvan T. Coloja, placed in the public domain by the author.
"""

import sys

MAGIC_WORD = 'abandon'

def main(argv):
    with open(argv[1], 'r') as faddr:
        addresses = set(l.rstrip() for l in faddr)
    with open(argv[2], 'r') as fres:
        the_text = set(l.rstrip() for l in fres)

    for address in addresses:
        for line in the_text:
            if line.startswith(address):
                where_found = line.find(MAGIC_WORD)
                if where_found != -1:
                    if where_found > 50:
                        start_excerpt = where_found - 50
                    else:
                        start_excerpt = 0
                    print '"%s","%s"' % (
                        address,
                        line[start_excerpt:where_found + len(MAGIC_WORD) + 50])


if __name__ == '__main__':
    main(sys.argv)
  • 1
    Take a look at this : https://stackoverflow.com/questions/13939038/how-do-you-run-a-command-for-each-line-of-a-file It might help you . – Mark Davydov Nov 18 '17 at 18:59
  • 1
    How about showing sample data for BOTH files, and the desired output? So far it's mostly guesswork for others. – tink Nov 18 '17 at 19:00
  • Sample data: **rezultate2.txt** `code`www.uvvg.ro/site/un-askenaz-intre-romania-si-eritreea/index.html-

    Eritreea (oficial Statul Eritreea) este un situat în partea de nord-est a Africii, mărginit de Sudan la nord și nord-vest, Etiopia la sud și vest, Djibouti la sud-est și Marea Roșie la est. Fostă colonie italiană și britanică, Eritreea a fost anexată de Etiopia în 1952 `code`

    – Razvan T. Coloja Nov 18 '17 at 19:03
  • The comment section doesn't allow me to write too many characters. The desired output would be the first line in adrese.csv followed by the corresponding grep ouput above (the word "abandon" and the 50 characters left and right of it). – Razvan T. Coloja Nov 18 '17 at 19:08
  • 1
    Just edit your original post ... – tink Nov 18 '17 at 19:09
  • Update your question with the relevant information rather than adding it in comments. – Ahmed Masud Nov 18 '17 at 19:12
  • Heh. And now edit the edit so it's actually readable ;D – tink Nov 18 '17 at 19:16
  • 1
    What you're asking for is, taken at face value, both insanely inefficient and utterly unnecessary. `grep` can take a list of patterns to use as input, so you can find all lines in file2 matching any line from file1 in just one pass. And if you want to do an efficient join, there are likewise join tools written just for the purpose that do a single pass through two sorted inputs, finding lines in common. – Charles Duffy Nov 18 '17 at 19:38

2 Answers2

0

If I got it right what you are trying to achieve (running a grep on adrese.csv for each line with "abandon" in rezultate2.txt) the following snippet should do just that:

grep -E -o ".{0,50}abandon.{0,50}" rezultate2.txt | while read LINE ; do
  grep "$LINE" adrese.csv >> output.csv
done
dabadab
  • 388
  • 1
  • 10
  • That would seem right. Thank you. But the script gives me the following error: `grep: Unmatched [ or [^ grep: Unmatched [ or [^ grep: Unmatched [ or [^ grep: invalid option -- '&' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. grep: invalid option -- '&' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information.` and output.csv is just about 10 lines long while adrese.csv is about 650+ lines long. – Razvan T. Coloja Nov 18 '17 at 19:16
  • Probably `rezultate2.txt` contains characters that are interpreted as regexp - if you do not want that use `-F` switch: `grep -F "$LINE" adrese.csv >> output.csv` – dabadab Nov 18 '17 at 19:20
  • Thanks for the suggestion. I forgot about -F. The thing is i still get the same error, even with -F: `cypress@aspire:~/Desktop$ sh script_final.sh grep: invalid option -- '&' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. grep: invalid option -- '&' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information.` – Razvan T. Coloja Nov 18 '17 at 19:26
  • @RazvanT.Coloja, sounds like you have a line that starts with a dash. Use `-e` before it, thus: `grep -F -e "$LINE"`. (Though I stand by my comment on the question that given an explanation of *why* you're trying to do this -- and thus what constraints you have, regarding ordering or otherwise -- we could come up with a vastly more efficient alternative). – Charles Duffy Nov 18 '17 at 19:39
  • Tried it. No error this time but the resulting **output.csv** is still small. I think I will run **html2txt** on **rezultate2.txt** to clean it up a bit. But thank you for your help guys. – Razvan T. Coloja Nov 18 '17 at 19:45
0

If I have understood you correctly, this:

sed 's/,/\n/g' adrese.csv | while read text ; do grep -F "$text" rezultate2.txt ; done > output.txt

Will:

  1. The sed will convert the csv file into single lines
  2. The while loop will take each of those inputs
  3. And grep through the file for them.
  4. Each of the output will be saved into output.txt