Restal Restal - 6 months ago 21
Linux Question

Match single digits only sed

I have to make a regex to match one digit only.
it should match 7 and a7b but not 77.
I made this but it doesn`t seem to work in sed.

(?<![\d])(?<![\S])[1](?![^\s.,?!])(?!^[\d])
(?<![\d])(?<!^[\a-z])\d(?![^a-z])(?!^[\d])


What am I doing wrong?

Edit:

I need to replace only 1-digit numbers with something like

sed 's/regex/@/g' file //regex to match "1"


file content

1 2 3 4 5 11 1
agdse1tg1xw
6 97 45 12


Should become

@ 2 3 4 5 11 @
agdse@tg@xw
6 97 45 12

Answer

To do what you show in the Example in your question is:

$ sed -r 's/(^|[^0-9])1([^0-9]|$)/\1@\2/g' file
@ 2 3 4 5 11 @
agdse@tg@xw
6 97 45 12

but that only works because you didn't have 1 1 in your data. If you did you'd need 2 passes:

$ echo '1 1' | sed -r 's/(^|[^0-9])[0-9]([^0-9]|$)/\1@\2/g'
@ 1

$ echo '1 1' | sed -r 's/(^|[^0-9])[0-9]([^0-9]|$)/\1@\2/g; s/(^|[^0-9])[0-9]([^0-9]|$)/\1@\2/g'
@ @

and if you wanted to do that for any single digit it would be:

$ sed -r 's/(^|[^0-9])[0-9]([^0-9]|$)/\1@\2/g; s/(^|[^0-9])[0-9]([^0-9]|$)/\1@\2/g' file
@ @ @ @ @ 11 @
agdse@tg@xw
@ 97 45 12
Comments