0

I'm trying to replace a field with the proper value but I can't figure out how to do a blanket substitution. Here is what I have so far.

sed -i '' 's/AuthUserName=\[7000-8000]/AuthUserName=7325/g' "$f"

I'm trying to search all the files that contain AuthUserName=7000-8000 and make them all say AuthUserName=7325. I can do it individually by just having the AuthUserName=7000 for example and it will replace it with 7325 but I can't figure it out.

I tried to use =* but no success. Can anyone help me out? Thanks in advance.

KunduK
  • 32,888
  • 5
  • 17
  • 41
Alex
  • 5
  • Possible duplicate of [Using regular expressions to validate a numeric range](https://stackoverflow.com/questions/22130429/using-regular-expressions-to-validate-a-numeric-range) – Wiktor Stribiżew Feb 13 '19 at 23:24
  • `perl -pe 'm/AuthUserName=([0-9]+)/; s//AuthUserName=7325/ if $1 >= 7000 && $1 <= 8000' input` – William Pursell Feb 14 '19 at 00:38

3 Answers3

0

For the numbers 7000 up to 7999 you could use a regular expression like 7[0-9][0-9][0-9]. Where each [0-9] simply stands for "one single digit from 0-9". You would then only have to treat the 8000 as a special case.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47
  • That makes a perfect sense. I tried AuthUserName=\7[0-9][0-9][0-9]/AuthUserName=7325 with no success. I am not too terribly familiar with regular expressions but I will do some studying after this problem. Mind showing me the proper syntax? – Alex Feb 13 '19 at 23:32
  • sed 's/AuthUserName=7[0-9][0-9][0-9]/AuthUserName=7325/g' – Dirk Herrmann Feb 13 '19 at 23:40
0

You can try something like

sed 's/AuthUserName=7[0-9][0-9][0-9]\|AuthUserName=8000/AuthUserName=7325/g'

or

sed 's/AuthUserName=7[[:digit:]][[:digit:]][[:digit:]]\|AuthUserName=8000/AuthUserName=7325/g'

[0-9] or [[:digit:]] are equivalent.

\| expresses alternative expression

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31
0

Using perl and lookaround. First a test file:

$ cat file
asd
asd AuthUserName=700
asd AuthUserName=7000
asd AuthUserName=8000
asd AuthUserName=9000
asd AuthUserName=70000

The perl one-liner:

$ perl -pe 's/(?<=AuthUserName=)(7[0-9]{3}|8000)(?![0-9])/7325/g' file

ie. 7000-8000 preceeded by AuthUserName= and not followed by a digit, output:

asd
asd AuthUserName=700
asd AuthUserName=7325
asd AuthUserName=7325
asd AuthUserName=9000
asd AuthUserName=70000

In sed:

$ sed 's/\(AuthUserName=\)\(7[0-9]\{3\}\|8000\)\([^0-9]\|$\)/\17325\3/g' file
James Brown
  • 36,089
  • 7
  • 43
  • 59
  • Thank you for the help. I'm not familiar with perl at all but i'm going to look into it now. – Alex Feb 14 '19 at 20:07