Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Space character in Sed

Tags:

regex

sed

I've tried almost everything (I guess) but nothing worked. (Operating System: Ubuntu 12.04)

Expressions to be matched (removed from text files):

a c 4
a k 23
o s 1

What I tried:

's/[[a-z][:space:][a-z][:space:][0-9]]\{1,\}//gi'
's/.\s.\s[0-9]+//g'
's/[:alpha:][:space:][:alpha:][:space:][:digit:]+'
like image 212
Mansueli Avatar asked Mar 19 '13 20:03

Mansueli


2 Answers

This should match:

sed 's/[a-z][ ]*[a-z][ ]*[0-9]*//gi'

Your 1st try misses a couple of square brackets, and you don't need the outermost one:

sed 's/[a-z][[:space:]][a-z][[:space:]][0-9]\{1,\}//gi' input

Your 2nd example fails because you need to escape the +, and still it will only work in gnu sed:

sed 's/.\s.\s[0-9]\+//g' input

Also some similar problems with the last one:

sed 's/[[:alpha:]][[:space:]][[:alpha:]][[:space:]][[:digit:]]\+//' input
like image 142
perreal Avatar answered Oct 27 '22 10:10

perreal


The one in the middle is close! You have to escape the plus sign for a reason that is beyond me. I also replaced the dot "." with "[a-z]" so it only matches letters.

sed 's/[a-z]\s[a-z]\s[0-9]\+//g'

Bonus portable version for older sed-Versions (Hello, Mac users!). Some sed implementations are picky on the "+" :

sed 's/[a-z]\s[a-z]\s[0-9][0-9]*//g'
like image 22
svckr Avatar answered Oct 27 '22 12:10

svckr