replace 0 with R at position 40


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers replace 0 with R at position 40
# 1  
Old 06-08-2009
replace 0 with R at position 40

i have this contents:

H,0100, 4022,FISHER810,6109566,,,,,0,562032,
D,0100, 4022,FISHER810,6109566,,1,,,0,562032

if i see 0 at position 40, i want to replace with R.
# 2  
Old 06-08-2009
try awk
Code:
awk '{substr($0,40,1) ~ /0/ }{sub(substr($0,40,1),"R")}{print}' filename

# 3  
Old 06-08-2009
thanks.

sed 's/^........................................0/^........................................R'/g worked fine.

-----Post Update-----

actually, i want to replace 0 with R at col10, instead of postion 40.

-----Post Update-----

i tried this code: awk '{substr($0,40,1) ~ /0/ }{sub(substr($0,40,1),"R")}{print}' filename

did not replace 0 with R.
# 4  
Old 06-08-2009
if you have Python
Code:
import fileinput
for line in fileinput.FileInput("file",inplace=1):
    line=list(line.strip())
    if line[39]=="0":
        line[39]="R"
    print ''.join(line)

# 5  
Old 06-09-2009
no, i don't.
# 6  
Old 06-09-2009
Quote:
Originally Posted by tjmannonline

actually, i want to replace 0 with R at col10, instead of postion 40.

i tried this code: awk '{substr($0,40,1) ~ /0/ }{sub(substr($0,40,1),"R")}{print}' filename

did not replace 0 with R.
You can't use the same code to replace another column. Why don't try to understand the code of vidyadhar8 and adjust the code yourself?

Regards
# 7  
Old 06-09-2009
vidyadhar8,

if you don't mind, would you please explain $0, 40, 1, and ~
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

2. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

3. Shell Programming and Scripting

position specific replace in file

How to replace the position specific values in the file.. i searched a lot the forums but i couldn't able to do... i have file like below 576666666666666666666666666 7878 897987 121 0asdas Y12 5900fbb 777 09JJJ 78798347892374 234234234364 234232898 89HJHIHIGIUG989902743748327khjkhkjlh... (6 Replies)
Discussion started by: greenworld123
6 Replies

4. Shell Programming and Scripting

Search and Replace by record position

Hi All, I have a file that I would like to search for data and replace other data by record position number: Example search.. search for "CLARK KENT" and replace Amt data "000025" with "000155"??? I'm able to search and replace unique data but, came to a stump when wanting to replace data... (11 Replies)
Discussion started by: macastor
11 Replies

5. 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

6. Shell Programming and Scripting

Sed position specific replace

I'm drawing a blank on how to use sed to replace selectively based on position in the string (vs nth occurence): hello.|there.|how.|are.|you.| I want the period removed in the 3rd item (as defined by the pipe delimiter) if a period is present. So the result in this case would be: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

7. UNIX for Dummies Questions & Answers

Replace based on an exact position

Trying to use sed - but having no luck. I have a text file - I want to replace whatever character is in position 106, 157 and 237 w/ the string "xxx". Want this change for all lines w/in that text file. I'm open to using awk or whatever command would be best for replacing characters based... (5 Replies)
Discussion started by: svn
5 Replies

8. UNIX for Dummies Questions & Answers

replace position

my file: S10000000146981+0000003113079+0000000096981 i'd like to copy position 31-44, and paste onto position 3-16 i'd like to replace position 31-44 with 0 (zero) my final file should look like this: S10000000096981+0000003113079+0000000000000 thanks in advanced. (10 Replies)
Discussion started by: tjmannonline
10 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

How to replace symbols by position using SED

Hi all, I need your help. For example I have string in file.txt: -x -a /tmp/dbarchive_NSS_20081204 -f 900 -l 1 2008/12/04 2008/12/04 So, I need to replace symbols from (for e.g.) position 26 till 33 with symbols which I have in file replace.txt And I have no idea how to do it. If... (1 Reply)
Discussion started by: nypreH
1 Replies
Login or Register to Ask a Question