Find character and Replace character for given position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find character and Replace character for given position
# 1  
Old 12-03-2012
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
# 2  
Old 12-03-2012
How about this thread
# 3  
Old 12-03-2012
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


Code:
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?

Last edited by Scott; 12-04-2012 at 01:04 PM.. Reason: Code tags
# 4  
Old 12-03-2012
If this file is in this fixed format, this would work:

Code:
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:

Code:
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

This User Gave Thanks to in2nix4life For This Post:
# 5  
Old 12-03-2012
Quote:
Originally Posted by Sara183
. . .
And i need to find if '-' present in that string B090712-0013001, then replace the whole string with "O".
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:
Code:
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

Quote:
Did anyone know the how to achieve this?
Bumping up threads within few hours DOES NOT REALLY help.
# 6  
Old 12-03-2012
Code:
sed 's/^\(.\{255\}.\{34\}\)\(.\{43\}\)-/\1OOOOOOOOOOOOOO/'

# 7  
Old 12-04-2012
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,

Code:
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
Code:
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.

Last edited by Scott; 12-04-2012 at 01:05 PM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

3. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Find position of character with awk

Hi Guys! Could anyone help me with?.. I have a line which says BCVGF%6$#900 .....How can we know which position is for % or say $ by command or script?There is any way to get a prompt by any script? Thanks a lot (6 Replies)
Discussion started by: Indra2011
6 Replies

5. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

6. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

7. Shell Programming and Scripting

How to find character position in file?

how to find character positionin file? i.e string = "123X568" i want to find the position of character "X". Thanks (6 Replies)
Discussion started by: LiorAmitai
6 Replies

8. Shell Programming and Scripting

Replace character in certain position in a string

Hello everyone this is my first post of many to come :) I am writing a script and in this script at one point i need to replace a character in a particular position in a string for example: in the string "mystery" i would need to replace the 3rd position to an "r" so the string becomes... (3 Replies)
Discussion started by: snipaa
3 Replies

9. UNIX for Dummies Questions & Answers

How to replace character on defined position

I need to replace the character on 6th position. If this character is 1 I have to repleace it with A, if it is 2 than I have to replace it with B. If it is not 1 or 2 I should not repleace it. input: abcd defg abcd 1efg mnop weac rstu 2bcd i need: abcd defg abcd Aefg mnop weac rstu... (2 Replies)
Discussion started by: necroman08
2 Replies

10. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies
Login or Register to Ask a Question