Find and replace with 0 for characters in a specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace with 0 for characters in a specific position
# 1  
Old 05-29-2014
Find and replace with 0 for characters in a specific position

Need command for position based replace:
I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file.

For example the file ABC.txt has:
Code:
abcdefghijklmnopqrstuvwxyz
6abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
6zyxwvutsrqponmlkjihgfedcba

In the above example the line 2 "jklmnopqrs" should be replace with 0s; and in line 4 "qponmlkjih" should be replaced with 0s.
The expected output (should be copied to different file i.e. ABC.txt to XYZ.txt):
Code:
abcdefghijklmnopqrstuvwxyz
6abcdefghi0000000000tuvwxyz
abcdefghijklmnopqrstuvwxyz
6zyxwvutsr0000000000gfedcba

Please help..

Last edited by Scrutinizer; 05-29-2014 at 03:16 PM.. Reason: To provide clear example; [mod] code tags
# 2  
Old 05-29-2014
One way:
Code:
sed '/^6/s/.\{10\}/0000000000/2' file

Some awks (GNU awk, BSD awk, mawk) can do this:
Code:
awk '/^6/{for(i=11; i<=20; i++) $i=0}1' FS= OFS= file

gawk 4:
Code:
awk '/^6/{gsub(/./,0,$2)}1' FIELDWIDTHS="10 10 100" OFS= file


Last edited by Scrutinizer; 05-29-2014 at 03:59 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-29-2014
Code:
perl -ple '/^6/ and substr( $_, 10, 10) = "0000000000"' file

This User Gave Thanks to Aia For This Post:
# 4  
Old 05-29-2014
Code:
$ cat file
abcdefghijklmnopqrstuvwxyz
6abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
6zyxwvutsrqponmlkjihgfedcba

Code:
$ awk '/^6/{gsub(substr($0,11,10),sprintf("%010d",0))}1' file

OR

$ awk '/^6/{$0 = substr($0,1,10) sprintf("%010d",0) substr($0,21)}1' file

OR

$ awk '/^6/{n=split($0,A,r);for(i=1;i<=n;i++) printf("%s",i>10 && i<=20 ? 0 : A[i]);printf RS;next}1' file

abcdefghijklmnopqrstuvwxyz
6abcdefghi0000000000tuvwxyz
abcdefghijklmnopqrstuvwxyz
6zyxwvutsr0000000000gfedcba

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 05-29-2014
Thank you all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Need help on find and replacement on specific line and position

I have a file with 100 lines. On 50 th line , from position 5 to rest of the data , I need to change the occurrence of A to B and Occurrence of M to N. Input file : Line1 Line2 Line3 -- -- 12345ABCDEFM --- -- Line 100 Output Line1 Line2 (40 Replies)
Discussion started by: Rajesh_us
40 Replies

3. Shell Programming and Scripting

Printing characters at specific position in line

Hi, I am trying to get an output like : +----------------------------------+ ----------- + + some variable substitution + some text + Is there a way I can specify in printf (in ksh) the particular position I want to print a character, and also repeat a character from... (1 Reply)
Discussion started by: neil.k
1 Replies

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

5. Shell Programming and Scripting

Awk command to replace specific position characters.

Hi, I have a fixed width file. The way this file works is say for example there are 30 columns in it each with different sizes say 10,5,2, etc... If data in a field is less than the field size the rest of it is loaded with spaces. I would like an awk command to that would replace I have... (8 Replies)
Discussion started by: pinnacle
8 Replies

6. Shell Programming and Scripting

Using sed to replace a string in a specific position

I asked this before, but my problem got more complicated. Heres what I am trying to do: I'm trying to replace a string at a certain location with another string. Heres the file I'm trying to change: \E I want to replace the escape code at the 3rd line, 2nd column with this escape code... (3 Replies)
Discussion started by: tinman47
3 Replies

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

8. Shell Programming and Scripting

Add characters at specific position in file

Hello I want to add some value at the specific position. My file has data like Hello Welcome to UNIX Forums Need Assistance I want to add some value at the end but at same character position for all lines. I want my output file to have data like : Here '_' represents blanks.... (3 Replies)
Discussion started by: dashing201
3 Replies

9. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

10. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies
Login or Register to Ask a Question