[Solved] Extract variable names from file [closed]


I don’t know about sed or R, but in Python:

>>> import re
>>> i = """NOTE: Variable Variable_S1 already exists on file D1.D, using Var_S8 instead.
NOTE: The variable name more_than_eight_letters_m has been truncated to ratio_s.
NOTE: Variable ratio_s already exists on file D1.D, using Var_S9 instead."""
>>> print(re.findall(r'(\w+_\w+)', i))
['Variable_S1', 'Var_S8', 'more_than_eight_letters_m', 'ratio_s', 'ratio_s', 'Var_S9']

Here is an improved version, which will give you the set of variables for each line:

>>> print([re.findall(r'(\w+_\w+)', line) for line in i.split('\n')])
[['Variable_S1', 'Var_S8'],
 ['more_than_eight_letters_m', 'ratio_s'],
 ['ratio_s', 'Var_S9']]

solved Extract variable names from file [closed]