Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing Characters 1

Status
Not open for further replies.

longs

Programmer
Jul 27, 2007
31
US
Hi All!

I got a small problem. let me explaing myself here.

i have a flat file;
ab1234567890cdefghj
AB8373839112CDEFGHI
MN2348897345KJSDFKL

They are fixed length lines. I want to replace characters from 3rd place to 8th with 0's so my out file look like;
ab0000000090cdefghj
AB0000000012CDEFGHI
MN0000000045KJSDFKL

I am trying to use sed 's/?/?/'falt_file >flat_file.new but im not sure how to define the 3rd position to 8th position. like sed 's/^./0/' falt_file > flat_file.new will give us new flat file with its first character replace by 0.
Can any help me please?

Thanks
 
You may try this:
awk '{print substr($0,1,2)"00000000"substr($0,11)}' flat_file >flat_file.new

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
And as you asked for a sed way:
sed 's!\(..\)........!\100000000!' flat_file >flat_file.new

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PH That was great. Can you please explain a bit on this solution? That will be highly appreciated!!!
Regards
Longs
 
As I understand sed 's!\(..\)........!\100000000!'

"!\(..\)" means not the first two
"........" means 3rd to 8th
"!\1" That part i dont understand
"00000000" this is the replacement part
"!" at the end i dont understand as well .

If you can help me understnd that will be great

Thanks alot for your help once more
Longs
 
man is your friend:
man sed

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top