LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-26-2013, 06:41 AM   #1
lwsunny
LQ Newbie
 
Registered: Jul 2013
Posts: 3

Rep: Reputation: Disabled
How to use sed replace matching string of line contents at fixed position


hi all, i need to loop each lines of a file, and look up if there is a matching word at specific position, eg. at position 6= ABC;
then replace the words at position 10 with DEF;
thanks for any help!!
e.g.
input_file.txt
123ABC789JKL123
123ABC789MNL123
123FGH789PQR123

output_file as:
output.txt
123ABC789DEF123
123ABC789DEF123
123FGH789PQR123
 
Old 07-26-2013, 07:16 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,333

Rep: Reputation: Disabled
Basically, you search for the following pattern (pseudo-code):
<start of line>(<X random characters>)ABC(<y random characters>)<3 random characters>(<rest of line>)
The parts in parentheses must be stored for backreferencing. Now replace with:
(backref 1)ABC(backref 2)DEF(backref 3)
The following sed command will replace characters 10-12 with "DEF" in any string where characters 4-6 are "ABC":
Code:
sed 's/^\(.\{3\}\)ABC\(.\{3\}\).\{3\}\(.*\)/\1ABC\2DEF\3/'
 
1 members found this post helpful.
Old 07-26-2013, 07:23 AM   #3
lwsunny
LQ Newbie
 
Registered: Jul 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
thanks for quick reply!

i am new to linux script, how can i loop each line of the input file, then output the replace one to another file...
 
Old 07-26-2013, 07:35 AM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,333

Rep: Reputation: Disabled
That's a very common question, and it has been answered here on LQ quite a few times.

Most would recommend using a while loop and input redirection:
Code:
while read x ; do
  [... process $x here and write the results to another file ...]
done < input_file
A quick search of the LQ forums should turn up a few examples.
 
Old 07-26-2013, 08:03 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@lwsunny: These bash related links might come in handy:
 
Old 07-31-2013, 08:50 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by lwsunny View Post
i am new to linux script, how can i loop each line of the input file, then output the replace one to another file...
No need for an explicit loop. You may convert an entire input file with a single line of code.

Using sed, as coded by Ser Olmy:
Code:
sed 's/^\(.\{3\}\)ABC\(.\{3\}\).\{3\}\(.*\)/\1ABC\2DEF\3/' $InFile >$OutFile
Using awk, as coded by yours truly:
Code:
awk 'BEGIN{FS=OFS=""} {if (substr($0,4,3)=="ABC") {$10="DEF";$11=$12=""}}1' $InFile >$OutFile
Daniel B. Martin
 
  


Reply


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SED: replace all coincidences only from a position upto another jmvidalvia Programming 9 02-05-2013 04:11 PM
[SOLVED] sed: global search and replace if a string isn't anywhere in that line linux_kung_fu Linux - General 5 03-09-2012 10:53 AM
[SOLVED] replace specific position with sed nushki Programming 6 01-27-2012 04:36 AM
[SOLVED] SED search and replace fields in a fixed position based on a condition. jfkse7en Programming 7 01-06-2012 06:14 AM
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 07:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:47 AM.

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