LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   replace specific character after specific line by awk (http://www.linuxquestions.org/questions/programming-9/replace-specific-character-after-specific-line-by-awk-820733/)

Syed Tarique Moin 07-19-2010 12:33 PM

replace specific character after specific line by awk
 
Hello,

I want to replace specific character in a file after every specific line.

example as follows.

O 000000000000000000
A 111111111111111111
C 222222222222222222
D 333333333333333333
O 000000000000000000
A 111111111111111111
C 222222222222222222
D 333333333333333333

I want to replace only O with T after specific interval starting from line 1 as can be seen from here. I would be highly grateful for the valuable suggestion or the way how to do this.

Regards

pixellany 07-19-2010 12:48 PM

Looking at your example, we could guess that the code could match a string of zeros, or simply "O" at the beginning, or Line numbers 1,5,9,13...etc.

Here's three solutions---one for each option:

Code:

sed '/0000000/s/O/T' filename > newfilename

sed 's/^O/T/' filename > newfilename

sed '1,+4s/^O/T/' filename > newfilename


ghostdog74 07-19-2010 02:47 PM

Code:

awk '$1=="O"{$1="T"}1' file


All times are GMT -5. The time now is 03:15 AM.