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
anbu23
March 11, 2010, 5:04pm
2
sed "s/^\(.\{5\}\)2D3M/\12A3M/" file
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..
anbu23
March 11, 2010, 5:31pm
4
sed code will check 2A3M only from 6th to 9th position in the file.
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. \1 gives the first five characters saved in \( \)
gr8..thanks/...it worked..)
Sed is better in this case, here is the awk code.
awk -F "" '{if (substr($0,6,4)=="2D3M") $7="A"}1' OFS="" samp.txt