Abhijit Shinde
Abhijit Shinde

Reputation: 11

grep string with special characters in file

my file contains:

/*uid:68160*/\n            SELECT
/*uid:68160*/SELECT

I tried with below:

grep -vF "/[*]uid::[[:digit:]][*]/SELECT"

which is helping to removed 2nd line. How to remove 1st line by grep also tried:

grep -vF "/[*]uid::[[:digit:]][*]/\n            SELECT"

Upvotes: 1

Views: 729

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

Assuming you have a literal text like that,

s='/*uid:68160*/\n            SELECT
/*uid:68160*/SELECT
Text'

and you want to remove lines 1 and 2, you may use

 grep -Ev '/[*]uid:[[:digit:]]+[*]/(\\n *)?SELECT'

See the online grep demo

Details

  • -Ev - E enables POSIX ERE and v will negate the result
  • /[*]uid:[[:digit:]]+[*]/(\\n *)?SELECT - matches
    • /[*]uid: - a /*uid: string
    • [[:digit:]]+ - 1+ digits
    • [*]/ - a */ string
    • (\\n *)? - an optional group matching 1 or 0 occurrences of \n two-char combination and then any 0 or more spaces
    • SELECT - a string

Upvotes: 1

Related Questions