Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-11-2010
Registered User
 

Join Date: Aug 2009
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Find and replace a string a specific value in specific location in AIX

Hi,

I have following samp.txt file in unix.

samp.txt

01Roy2D3M000000
02Rad2D3M222222
.
.
.
.
10Mik0A2M343443

Desired Output

01Roy2A3M000000
02Rad2A3M222222
.
.
.
.
10Mik0A2M343443

I want to find a value for 2D3M in the 6,7,8,9 positions from the beginning of record, if found need to replace it with 2A3M

Thanks
Sponsored Links
    #2  
Old 03-11-2010
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,849
Thanks: 3
Thanked 45 Times in 44 Posts

Code:
sed "s/^\(.\{5\}\)2D3M/\12A3M/" file

Sponsored Links
    #3  
Old 03-11-2010
Registered User
 

Join Date: Aug 2009
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Hi anbu23,

Thanks for quick reply..so this will check for the string only in the 6, 7, 8, 9 positions only right..and it should not check any other positions of every records of the file..

sed "s/^\(.\{5\}\)2D3M/\12A3M/" file

what does 1 mean before 2A3M in the above SED command.

Pls respond..thanks..

Last edited by techmoris; 03-11-2010 at 05:24 PM..
    #4  
Old 03-11-2010
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,849
Thanks: 3
Thanked 45 Times in 44 Posts
sed code will check 2A3M only from 6th to 9th position in the file.

Quote:
Save the pattern enclosed between \( and \) in a special holding space. Up to nine subpatterns can be saved on a single pattern. The text matched by the subpatterns can be reused later in the same pattern, by the escape sequences \1 to \9. For example, \(ab\).*\1 matches two occurrences of ab, with any number of characters in between.
\(.\{5\}\) Holds first five characters. [COLOR="rgb(255, 0, 255)"]\1[/COLOR] gives the first five characters saved in [COLOR="rgb(255, 0, 255)"]\( \)[/COLOR]
Sponsored Links
    #5  
Old 03-11-2010
Registered User
 

Join Date: Aug 2009
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
gr8..thanks/...it worked..)
Sponsored Links
    #6  
Old 03-11-2010
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 

Join Date: Jun 2006
Posts: 2,632
Thanks: 43
Thanked 383 Times in 373 Posts
Sed is better in this case, here is the awk code.

Code:
awk -F "" '{if (substr($0,6,4)=="2D3M") $7="A"}1' OFS="" samp.txt

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to append a string to a specific location in a line mwrg Shell Programming and Scripting 4 11-18-2009 02:28 PM
Finding a word at specific location in a string swapnil.nawale Shell Programming and Scripting 1 09-14-2009 08:49 AM
using sed to replace a specific string on a specific line number using variables todd.cutting Shell Programming and Scripting 2 08-13-2009 09:40 PM
find pid of process run in specific location JCR Shell Programming and Scripting 4 07-29-2009 04:25 AM
How to replace a specific word in specific column? ./hari.sh Shell Programming and Scripting 6 03-07-2009 08:33 AM



All times are GMT -4. The time now is 03:38 AM.