Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete a combination of string with sed

Tags:

linux

bash

sed

I delete some strings in the lines of a text with sed command:

sed 's:\(pattern\.\)::g' file

But certain patterns have letters and numbers:

sed 's:\(pattern\.\|pattern1\.\|pattern2\.\|pattern23\.\|patternx\.\|patternYx\.\)::g' file

a long etc...

How to delete pattern (always is the same) + numbers (any combination 0-9) and letters (A-Z, a-z) in one single line, to avoid repeat "pattern+something"

Thanks

like image 829
acgbox Avatar asked Mar 31 '26 06:03

acgbox


1 Answers

To remove pattern followed by zero or more numbers or letters followed by ., use:

sed 's:pattern[[:alnum:]]*\.::g'

Notes:

  1. The regex doesn't use the parens grouping, \(...\), so I removed it.

  2. [[:alnum:]] matches any letter or number.

  3. [[:alnum:]]* matches zero or more of any letter or number.

like image 95
John1024 Avatar answered Apr 02 '26 21:04

John1024



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!