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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-26-2013, 06:41 AM
|
#1
|
LQ Newbie
Registered: Jul 2013
Posts: 3
Rep: 
|
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
|
|
|
07-26-2013, 07:16 AM
|
#2
|
Senior Member
Registered: Jan 2012
Distribution: Slackware
Posts: 3,349
Rep: 
|
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.
|
07-26-2013, 07:23 AM
|
#3
|
LQ Newbie
Registered: Jul 2013
Posts: 3
Original Poster
Rep: 
|
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...
|
|
|
07-26-2013, 07:35 AM
|
#4
|
Senior Member
Registered: Jan 2012
Distribution: Slackware
Posts: 3,349
Rep: 
|
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.
|
|
|
07-26-2013, 08:03 AM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
@lwsunny: These bash related links might come in handy:
|
|
|
07-31-2013, 08:50 PM
|
#6
|
Senior Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,895
|
Quote:
Originally Posted by lwsunny
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
|
|
|
All times are GMT -5. The time now is 09:48 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|