Python Forum
Extract a string between 2 words from a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract a string between 2 words from a text file
Hi Forum,

I have a very long Code script containing SAS Proc SQL. There's a lot of excess code that I don't need and I know that the individual scripts will begin with 'proc sql' and end with 'quit;'.

I've read up and tried a few things but can't get any useful results.

Can anyone give me an idea of a Python3 script that would output a list the individual scripts using a .txt file?

Thanks Peter
pseudo code
p = path to script dir
python_filelist = [x for x in p.iterdir() if x.is_file() and x.suffix == '.py']
# or if you only want file names without paths:
python_filelist = [ for x in p.iterdir() if x.is_file() and x.suffix == '.py']
from pathlib import Path
import os

p = Path('.')

python_filelist = [x for x in p.iterdir() if x.is_file() and x.suffix == '.py'
    and not == __file__]

for file in python_filelist:
So you have a large file with code and you want to extract only "proc sql" parts to a text file.
I would read the lines from the source file one by one and check for a line containing "proc sql". When found, toggle a boolean variable, say "do_copy" to True and as long as this variable is true, copy the lines to your target file.
And also check for lines containing "quit". In that case toggle the boolean to "False" so the copying stops.

sourcefile = "venv/file1.txt"   # Replace this with your source file.
targetfile = "venv/file2.txt"   # Replace this with your target file

do_copy = False     # Boolean: copy line to target or not

with open(sourcefile, "r") as source, open(targetfile, "w") as target:
    for sourceline in source:
        if "proc sql" in sourceline.lower():
            do_copy = True
        if do_copy:
        if "quit" in sourceline.lower():
            do_copy = False

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 277 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Form that puts diacritics on the words in the text Melcu54 13 668 Aug-22-2023, 07:07 AM
Last Post: Pedroski55
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 516 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Pulling Specifics Words/Numbers from String bigpapa 2 451 May-01-2023, 07:22 PM
Last Post: bigpapa
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 570 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 609 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  extract only text strip byte array Pir8Radio 7 1,574 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Extract only certain text which are needed Calli 26 3,650 Oct-10-2022, 03:58 PM
Last Post: deanhystad
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 903 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extract text rektcol 6 1,267 Jun-28-2022, 08:57 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020