Reputation: 1
Can someone please tell me how I can replace a entire string with only partial? For Example if the txt file contains:
The number of water molecues: h20=20e43
And I need it to say:
The number of water molecues: h20=10e54
What I have so far is sed 's/h20/h20=10e54/'
, which outputs:
The number of water molecues: h20=10e54=20e43
The problem is that the original value I want to replace is not always the same so I need it to search for the patter h20=
only and replace the whole thing.
Upvotes: 0
Views: 2016
Reputation: 246744
Try: s/\(h20=\)[[:alnum:]]*/\110e54/
matches "h20=" (and remembers the value) followed by some alphanumeric chars; replaces with the remembered value (\1
) plus the literal string you want.
Also: perl -pe 's/h20=\K\w+/10e54/'
Upvotes: 1
Reputation: 41446
awk
can also be used:
echo "The number of water molecues: h20=20e43" | awk '{sub(/h20=.*/,"h20=10e54")}1'
The number of water molecues: h20=10e54
Upvotes: 0
Reputation: 45223
following your own idea, here is the fix.
echo "The number of water molecues: h20=10e54" |sed 's/h20=.*/h20=10e54/'
Upvotes: 0