homerjay
homerjay

Reputation: 157

Regular Expression to Find/Replace in a Fix Length File

So I have this big file of fix length lines. I want to do a find and replace on a character line position.

Example:

xxxxxxx     010109 xxxxxx xxxxx
xxxxxxx     010309 xxxxxx xxxxx
xxxxxxx     021506 xxxxxx xxxxx
xxxxxxx     041187 xxxxxx xxxxx

So in this case I would want to find any value starting on position 13 through position 18 and replace it with 010107.

Can anyone give help me out on how to formulate the regex for this?

Much appreciated.

Upvotes: 0

Views: 3661

Answers (5)

AmbroseChapel
AmbroseChapel

Reputation: 12097

Just for the record, you don't need a regular expression for something like this. A simple split, or some kind of unpack function, would be just fine.

Upvotes: 0

simon
simon

Reputation: 7022

Something like this:

sed 's/^\(.\{12\}\).\{6\}\(.*\)$/\1010107\2/'

should do the trick (escaped for command line use)

Upvotes: 0

qux
qux

Reputation: 1165

s/^(?:.{12})(.{6})(?:.*)$/NNNNNN/

replacing NNNNNN by the desired number

Upvotes: 0

Chad Birch
Chad Birch

Reputation: 74588

Edited: after testing, Notepad++ doesn't support the {n} method of defining an exact number of chars

This works, tested on your data:

Find:

^(............)......

Replace:

\1010107

Upvotes: 1

Gumbo
Gumbo

Reputation: 655499

Try this search pattern:

^(.{12})\d{6}

And this as replacement expression:

\1010107

Upvotes: 0

Related Questions