LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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



Reply
 
Search this Thread
Old 05-29-2009, 02:21 PM   #1
anoopvraj
LQ Newbie
 
Registered: May 2009
Posts: 2

Rep: Reputation: 0
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 ....??
 
Old 05-29-2009, 02:59 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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".
 
Old 05-30-2009, 03:55 AM   #3
anoopvraj
LQ Newbie
 
Registered: May 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
Old 05-30-2009, 04:34 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
But you can always use substr in awk.

Last edited by colucix; 05-30-2009 at 04:38 AM.
 
Old 05-30-2009, 04:57 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,487

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
Easy enuff to do in sed as well as it happens - but probably clearer in awk/perl.
 
Old 05-30-2009, 08:57 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by anoopvraj View Post
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
 
Old 05-30-2009, 08:59 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
PS: Awk does not need spaces---you can specify the delimiter.

Really good SED and AWK tutorials here:

http://www.grymoire.com/Unix/
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 04:01 PM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 11:02 PM
sed replace string octeto Programming 4 06-06-2007 03:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 11:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 09:42 PM


All times are GMT -5. The time now is 09:02 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration