I have scenario where i want to replace the digit or string
after equal to =
to null [ blank ]
Without creating new file need to replace in the same file
Data in file :
name=vilas
age=21
code=1345
Need to replace the digit after code=1345
to code=
I have tried this but stuck
sed -i 's/^code=$/code=/1g' file.txt
Note : The value after code= is going to be dynamic need to use regex pattern match which i am not good with
CodePudding user response:
var1=456
$ sed -Ei "s/(code=).*/\1$var1/" file
name=vilas
age=21
code=456
CodePudding user response:
Based on your question:
On MacOS and FreeBSD
sed -i '' -E 's/^code=[[:digit:]] $/code=/g' test1.txt
On CentOS and Debian
sed -i -E 's/^code=[[:digit:]] $/code=/g' test1.txt
You can update the character class to fit your needs.
where
-i Edit files in-place
-E Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's).
s The substitute command
g Apply the replacement to all matches to the regexp, not just the first.
GNU docs: https://www.gnu.org/software/sed/manual/html_node/Character-Classes-and-Bracket-Expressions.html SED docs: https://www.gnu.org/software/sed/manual/sed.html