Reputation: 63
I was writing a bash script to change all the B'10' values to B'12' in files.
So I have a file where B'10' is mentioned many times. And it can also be B'1010101010" with different length. All this has to be B'12121212". I tried to change with SED command which is :
sed -i -r "/[B'][10]+/s/10/12/g" filename
sed -i -r "/[B'][[0-9][0-9]]*[10]+/s/10/12/g" filename
I had to specify it twice, for only match B'10' and many B'1010101010..". If I only specified the second command, it was ignoring the single B'10' matches. So, This command is changing the values but it is changing for all the "10" matches it can find. But I need to change only after exactly B and single column near B character.
All the help is appreciated!! Thank you.
Upvotes: 0
Views: 129
Reputation:
try it by gnu sed before using -i option;
sed -E ":s s/\b(B')((12)*)10(10|\"|')/\1\212\4/ ;ts" filename
Upvotes: 1
Reputation: 50750
if your sed supports labels:
sed ':1 s/\(B\x27\(12\)*\)10/\112/; t1' file # or
sed -E ':1 s/(B\x27(12)*)10/\112/; t1' file
:1
label 1
,(B\x27(12)*)
matches B'
followed by zero or more 12
s, puts it into capturing group 1
,\1
expands to value kept in capturing group 1,t1
means "if a successful substitution is performed, go back to label 1
".Upvotes: 2
Reputation: 19982
When you want to change all 10
's after B'
, start with the last 10
in 1010...10
.
After changing the last, do it again and replace the new last 10
.
echo "B'1010101010111213" | sed -r ":a; s/B'((10)*)(10)/B'\112/; ta"
Upvotes: 0