Home > front end >  Replace digits using sed
Replace digits using sed

Time:03-02

I want all digits after a equal sign in a given string to be replaced with the text 'NULL'. I am new to using sed. How do I get the desired output using sed?

Example 1:

Input: /getProduct?productId=1234&quanity=4
Output: /getProduct?productId=NULL&quantity=NULL

Example 2:

Input: /getCustomers?customerId=432
Output: /getCustomers?customerId=NULL

Thank you.

CodePudding user response:

Using sed -r 's/(=)([0-9] )/\1NULL/g' inputfile.txt on inputfile:

/getProduct?productId=1234&quanity=4
/getCustomers?customerId=432
/getCustomers3?customerId=432

we get:

/getProduct?productId=NULL&quanity=NULL
/getCustomers?customerId=NULL
/getCustomers3?customerId=NULL

Only digits appearing after the equal sign are changed.

CodePudding user response:

Try

echo "/getProduct?productId=1234&quanity=4" | sed -E 's/[0-9] /NULL/g'
  • [0-9] : one or more digits
  • g: applies to all matches
  • Related