Find character and Replace character for given position

Hi,
i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command.

thanks in advance,
Sara

How about this thread

Hi,

I want to replace the whole position 284-298 to character 'O' if this '-' character occurs in that position. Above mentioned thread will replace only 289 position.

This is how my file will be

15628 27243 LM INS CORP                                                             QQ1234567977011 SPIDLE CONTRACTING LLC                                          21MI 10 13 2006 03 04 2010 05       Premium Recovered - First Attorney 00000050000-00000012000 00000000000 00000000000 B00000000399225
15628 27243 LM INS CORP                                                             QQ1234567977011 SOUTH CAROLINA HURRICANES                                       20SC 01 09 2011 09 15 2012 20                         Audit Adjustment 00000490000 00000000000 00000000000 00000000000 B090712-0013001

And i need to find if '-' present in that string B090712-0013001, then replace the whole string with "O".

---------- Post updated at 12:02 PM ---------- Previous update was at 09:56 AM ----------

Did anyone know the how to achieve this?

If this file is in this fixed format, this would work:

cat file

15628 27243 LM INS CORP QQ1234567977011 SPIDLE CONTRACTING LLC 21MI 10 13 2006 03 04 2010 05 Premium Recovered - First Attorney 00000050000-00000012000 00000000000 00000000000 B00000000399225
15628 27243 LM INS CORP QQ1234567977011 SOUTH CAROLINA HURRICANES 20SC 01 09 2011 09 15 2012 20 Audit Adjustment 00000490000 00000000000 00000000000 00000000000 B090712-0013001

perl -pe 's|\w{7}-\w{7}|('0' x 14).$1|ge' file

15628 27243 LM INS CORP QQ1234567977011 SPIDLE CONTRACTING LLC 21MI 10 13 2006 03 04 2010 05 Premium Recovered - First Attorney 0000000000000000002000 00000000000 00000000000 B00000000399225
15628 27243 LM INS CORP QQ1234567977011 SOUTH CAROLINA HURRICANES 20SC 01 09 2011 09 15 2012 20 Audit Adjustment 00000490000 00000000000 00000000000 00000000000 00000000000000

---------- Post updated at 12:27 PM ---------- Previous update was at 12:19 PM ----------

Revised:

perl -pe 's|\w{7}-\w{7}$|('0' x 14).$1|ge' file

15628 27243 LM INS CORP QQ1234567977011 SPIDLE CONTRACTING LLC 21MI 10 13 2006 03 04 2010 05 Premium Recovered - First Attorney 00000050000-00000012000 00000000000 00000000000 B00000000399225
15628 27243 LM INS CORP QQ1234567977011 SOUTH CAROLINA HURRICANES 20SC 01 09 2011 09 15 2012 20 Audit Adjustment 00000490000 00000000000 00000000000 00000000000 00000000000000

1 Like

This is NOT what you requested in the first place, where you requested a char-by-char replacement of chars in the region 284 - 298. Your sample file has 192 and 176 char lines.
Based on your initial question, in which you limited the solution to using sed , I'd proposed to try this:

sed -nr 's/^(.{283})/\1\n/; P;         # insert a <newline> after 283 char and print up to it
         s/^.{284}//; h                # remove first 283 chars plus <nl> and put remainder on hold
         s/^(.{15}).*/\1\n/;y/-/O/;P;  # prepare 15 chars (by dropping rest) and transliterate "-" to "O", print
         g                             # get back hold space
         s/^.{15}//;p                  # remove first 15 chars; then print
        ' file |
sed 'N;N;s/\n//g'                      # remove the two <newline> chars inserted and printed before

Bumping up threads within few hours DOES NOT REALLY help.

sed 's/^\(.\{255\}.\{34\}\)\(.\{43\}\)-/\1OOOOOOOOOOOOOO/'

Hi ,

To RudiC- My apologize, sample file i provided have spaces between them, that was eliminated by browser. I cannot able to understand your solution c'oz am new to SED.

To bipinajith - i tried your solution, but there was no change in output.

---------- Post updated at 04:05 PM ---------- Previous update was at 01:40 PM ----------

Hi in2nix4life,

Your solution worked great!! Thanks.

One more thing i want if this '-' character appears in different position like,

B0907120013-001
B09071200130-01
B090712001-3001

How the script will be?

---------- Post updated 12-04-12 at 11:56 AM ---------- Previous update was 12-03-12 at 04:05 PM ----------

I have tried this one

AWK=''' BEGIN { Pad = "O00000000000000"; } length <= 299 || ! index (substr ($0, 284, 15), "-") { print; next; } { print substr ($0, 1, 284) Pad substr ($0, 299); } '''
awk "${AWK}" tempfile

but nothing got changed my output. Am i doing anything wrong in this command. Please advice.

How about:-

awk ' { for(i=1; i<=NF; i++) { if(match($i,/-/)>0) gsub(/./,"O",$i); if(i==NF) printf "%s\n", $i; else printf "%s ", $i; } } ' tempfile
This one worked fine for my problem.
cat tempFile | sed -e "s/\-..............$/O00000000000000/g" > filea
cat filea | sed -e "s/.\-.............$/O00000000000000/g" > fileb
cat fileb | sed -e "s/..\-............$/O00000000000000/g" > filea
cat filea | sed -e "s/...\-...........$/O00000000000000/g" > fileb
cat fileb | sed -e "s/....\-..........$/O00000000000000/g" > filea
cat filea | sed -e "s/.....\-.........$/O00000000000000/g" > fileb
cat fileb | sed -e "s/......\-........$/O00000000000000/g" > filea
cat filea | sed -e "s/.......\-.......$/O00000000000000/g" > fileb
cat fileb | sed -e "s/........\-......$/O00000000000000/g" > filea
cat filea | sed -e "s/.........\-.....$/O00000000000000/g" > fileb
cat fileb | sed -e "s/..........\-....$/O00000000000000/g" > filea
cat filea | sed -e "s/...........\-...$/O00000000000000/g" > fileb
cat fileb | sed -e "s/............\-..$/O00000000000000/g" > filea
cat filea | sed -e "s/.............\-.$/O00000000000000/g" > fileb
cat fileb | sed -e "s/..............\-$/O00000000000000/g" > revised_file.
 
Thanks for everyone.

This will also do the magic,

cat $FILE | sed -e "s/B.[0-9]*[\-][0-9]*$/O00000000000000/g" > Temp_File