I am trying to replace 300th character and add a positive sign with decimal point accordingly. Sed command works fine perfectly for all characters except for B, F and {.
Please find the input data below:
result_PHDPTRAR2.txt
H009704COV2009084 PHD0000001H009700204COV2009084 PROD2015122016010418371304COVH009704COV2009084 PTR0000001H0097002C00000000140000000043610000003408092A0000000068061C0000000000000{0000002939340H0000000537585H0000003476926F0000001218378G0000000040292E0000000016497{0000000000827E0000001880498A9000000320436J000000004391000000001606000000000030000000000128000000000006000000004227000000000000000000000000 00000140 0000000000000{0000000000773B0000000000000{000000000000
Here 300th character is A. If we use following sed command , it works correctly for the above requirement:
sed -e 's/\(.\{1,255\}\)\(.\{1,34\}\)\(.\{1,9\}\)\(.*\)A/\1\2+\3.\4^/' <<< cat result_PHDPTRAR2.txt
It will replace A with ^ and get the following result.
H009704COV2009084 PHD0000001H009700204COV2009084 PROD2015122016010418371304COVH009704COV2009084 PTR0000001H0097002C00000000140000000043610000003408092A0000000068061C0000000000000{0000002939340H0000000537585H0000003476926F0000001218378G0000000040292E0000000016497{0000000000827E000+000188049.8^9000000320436J000000004391000000001606000000000030000000000128000000000006000000004227000000000000000000000000 00000140 0000000000000{0000000000773B0000000000000{000000000000
But the same commend does not work if we replace 300th character with B, F or {.
if i change 300th character of input(result_PHDPTRAR2.txt) with B and then if i use sed
sed -e 's/\(.\{1,255\}\)\(.\{1,34\}\)\(.\{1,9\}\)\(.*\)B/\1\2+\3.\4^/' <<< cat result_PHDPTRAR2.txt
i get following result :
H009704COV2009084 PHD0000001H009700204COV2009084 PROD2015122016010418371304COVH009704COV2009084 PTR0000001H0097002C00000000140000000043610000003408092A0000000068061C0000000000000{0000002939340H0000000537585H0000003476926F0000001218378G0000000040292E0000000016497{0000000000827E000+000188049.8B9000000320436J000000004391000000001606000000000030000000000128000000000006000000004227000000000000000000000000 00000140 0000000000000{0000000000773^0000000000000{000000000000
You can find +
and decimal point are added correctly in "+000188049.8B" but B remains same . Here B should be replaced with ^
Can anyone please help me?
The problem is that the first 'B' character in the input comes later than the 4..300 character. I.e. the input text doesn't match your expectations.
So, what now?
Update
Based on the comment, the problem is that there's more than 1 B in the text after the 300th character. The .* will go to that point. This is how to fix it:
sed -e 's/\(.\{1,255\}\)\(.\{1,34\}\)\(.\{1,9\}\)\([^B]*\)B/\1\2+\3.\4^/'
Watch out for the negated character class: \([^B]*\)B
- that will go up to the 1st B. Unfortunately, sed doesn't have non-greedy quantifiers. That would make it even more easy: \(.*?\)B
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments