LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 0
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
 
Old 07-19-2010, 11:48 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 07-19-2010, 01:47 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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


Reply



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 01:11 AM
Counting to a specific character mikehalfogre Programming 9 02-12-2010 02:12 PM
search and replace specific line in the file ablanc Programming 6 11-04-2009 11:06 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:50 PM.

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