sed command does not work for certain characters in unix

user1768029

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?

Tamas Rev

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

redis keys command does not work with certain characters

From Dev

redis keys command does not work with certain characters

From Dev

How does this sed command work?

From Dev

How does this sed command work?

From Dev

unix find command in terminal does not work

From Dev

How does the exit command work on a Unix terminal?

From Dev

unix find command in terminal does not work

From Dev

sed command does not work when a comma is present

From Dev

sed command does not work with find -exec?

From Dev

Using a negation in the sed command in UNIX

From Dev

Why does a sed command work interactively but not in my script?

From Dev

unix 'sort' command for inline characters

From Dev

Unix @ symbol does not work

From Dev

Does lookbehind work in sed?

From Dev

Sed regex does not work

From Dev

How to double digits using sed command in Unix?

From Dev

Unix sed command to replace brackets in file

From Dev

Unix sed command to replace brackets in file

From Dev

Unix bash Sed command Combination on one file

From Dev

Unix Shell scripting in AIX(Sed command)

From Dev

sed unix error: unterminated 's' command

From Dev

Unix: Adding and indenting using the sed command

From Dev

Unix command to get lines not containing certain text

From Dev

How to sed, awk, or tr certain characters in certain situations?

From Dev

How does this sed command works?

From Dev

What does this sed command mean?

From Dev

What does this sed command do?

From Dev

What does this sed command mean?

From Dev

sed error complaining "extra characters after command"