0

Consider the below set of lines in a file:

File1.csv

Apple is red WHERE the column ALP_ACCT_PROD_ID, it can be plucked
Apple is red ON ALP_ACCT_PROD_ID , WHERE it can be plucked

I want to look after if ALP_ACCT_PROD_ID occurs only after the word WHERE then only it should print it.

So for the above File it should print only the Line - Apple is red where the column ALP_ACCT_PROD_ID, it can be plucked

and ignore the second as ALP_ACCT_PROD_ID is not after WHERE.

Please help with the solution I am trying to do a normal grep and with that I am getting both the lines

grep ALP_ACCT_PROD_ID
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • See: [The Stack Overflow Regular Expressions FAQ](https://stackoverflow.com/a/22944075/3776858) – Cyrus Aug 27 '23 at 10:19
  • If `FOOWHEREBAR the column BOBALP_ACCT_PROD_IDSUE` existed in the input should that line be printed or not? Please include substrings, punctuation, regexp metachars, etc. in your sample input/output so we can see how you want them handled. It's always much easier to match lines you want than to not match similar lines you don't want. – Ed Morton Aug 27 '23 at 11:51

3 Answers3

1
grep 'WHERE.*ALP_ACCT_PROD_ID' File1.csv
  • WHERE - Literal match on WHERE.
  • .* - Match on . (any character) * times (zero or more times)
  • ALP_ACCT_PROD_ID - Literal match on ALP_ACCT_PROD_ID.
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

"... I want to look after if ALP_ACCT_PROD_ID occurs only after the word WHERE then only it should print it. ..."

Use the -E switch, to interpret the pattern as an "extended regular expression".

This will assert that if the value contains "ALP_ACCT_PROD_ID", it must be preceded by any number of characters, and the text, "WHERE".

grep -E 'WHERE\s+.+\s+ALP_ACCT_PROD_ID'
Reilas
  • 3,297
  • 2
  • 4
  • 17
0

In python should to be

import re

pattern = '^(.*)WHERE.*ALP_ACCT_PROD_ID'
text = 'Apple is red WHERE the column ALP_ACCT_PROD_ID, it can be plucked'
finded = re.match(pattern, text)

# this matches the first parenthesis, aka group
finded.group(1)

and if ALP_ACCT_PROD_ID comes after where the match is not valid.

Franz Kurt
  • 1,020
  • 2
  • 14
  • 14