Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-19-2010, 12:33 PM   #1
Syed Tarique Moin
LQ Newbie
Registered: Dec 2008
Posts: 13

Rep: Reputation: 0
replace specific character after specific line by awk


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.

Old 07-19-2010, 12:48 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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:

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

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

sed '1,+4s/^O/T/' filename > newfilename
Old 07-19-2010, 02:47 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
awk '$1=="O"{$1="T"}1' file


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replace pattern in specific lines and column with AWK cgcamal Programming 10 04-26-2010 02:11 AM
Counting to a specific character mikehalfogre Programming 9 02-12-2010 03:12 PM
search and replace specific line in the file ablanc Programming 6 11-05-2009 12:06 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 08:01 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration