LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 01: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, 01:59 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 02: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, 03:34 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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

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

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

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 07:59 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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

Really good SED and AWK tutorials here:

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


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
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM
sed replace string octeto Programming 4 06-06-2007 02:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:35 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