Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
|
|
05-29-2009, 02:21 PM
|
#1
|
LQ Newbie
Registered: May 2009
Posts: 2
Rep:
|
Using sed/awk to replace a string at a given position in
I have a sequential data file like this
1234570004400122222
1267570000800134567
...................
...................
and so on
Now I want to replace a pattern of 9 digits at the 6 th position with another patten,
say 700044001 with 700020050
I need to replace only if 700044001 occurs at 6th position, other wise no changes need be done.
How can I use 'sed' to do this ....??
|
|
|
05-29-2009, 02:59 PM
|
#2
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Homework??
Have you studied SED?
The general form you are looking for is: sed '/pattern/s/string/newstring/'
Translation: When it finds a line with "pattern", then it does one replacement on that line---replacing "string" with "newstring".
|
|
|
05-30-2009, 03:55 AM
|
#3
|
LQ Newbie
Registered: May 2009
Posts: 2
Original Poster
Rep:
|
Problem is that I dont want to change the first pattern into second pattern whenever it occurs. I want it to change it only when it occurs at the 6th column
sed -e 's/firstpattern/secondpattern/g' file
will replace all the occurences of first pattern with second pattern, I believe, and I dont want that to happen.
Since this is not a file separated by spaces as separate columns, I believe I cannot also use awk using $1, $2 etc.
|
|
|
05-30-2009, 04:34 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
But you can always use substr in awk.
Last edited by colucix; 05-30-2009 at 04:38 AM.
|
|
|
05-30-2009, 04:57 AM
|
#5
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,314
|
Easy enuff to do in sed as well as it happens - but probably clearer in awk/perl.
|
|
|
05-30-2009, 08:57 AM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Quote:
Originally Posted by anoopvraj
Problem is that I dont want to change the first pattern into second pattern whenever it occurs. I want it to change it only when it occurs at the 6th column
sed -e 's/firstpattern/secondpattern/g' file
will replace all the occurences of first pattern with second pattern, I believe, and I dont want that to happen.
Since this is not a file separated by spaces as separate columns, I believe I cannot also use awk using $1, $2 etc.
|
Read my post more carefully...
Quote:
The general form you are looking for is: sed '/pattern/s/string/newstring/'
|
Use "pattern" to decide which lines to modify. When sed finds "pattern", then it does the substitution according to what follows the "s" command
|
|
|
05-30-2009, 08:59 AM
|
#7
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
PS: Awk does not need spaces---you can specify the delimiter.
Really good SED and AWK tutorials here:
http://www.grymoire.com/Unix/
|
|
|
All times are GMT -5. The time now is 06:05 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
|
|