Wednesday, 18 September 2013

replace a string of characters with the line number

replace a string of characters with the line number

I have a text file that has approximately 3,000 lines. 99% of the time I
need all 3,000 lines. However, periodically I will grep out the lines I
need and direct the output to another text file to use.
The only problem I have in doing so, is: Embedded in the text file is a 6
character string of numbers that indicate the line number. In order to use
the file, this area needs to be correctly renumbered...(I don't need to
re-sort the data, but I need to replace the current six characters with
the new line number. and it must be padded with zeros! Unfortuantely the
entire rows is one long row of data with no field separators!
For example, my first three rows might look something like:
20130918082020ZZ000001RANDOMDATAFOLLOWSAFTERTHISABCDEFGH
20130810112000ZZ000999MORERANDOMDATAFOLLOWSAFTERTHISABCD
20130810112000ZZ000027SILLMORERANDOMDATAFOLLOWSAFTERTHIS
The six characters at positions 17-22 (Immediately following the "ZZ"),
need be renumbered based on the current row number...so the above needs to
look like:
20130918082020ZZ000001RANDOMDATAFOLLOWSAFTERTHISABCDEFGH
20130810112000ZZ000002MORERANDOMDATAFOLLOWSAFTERTHISABCD
20130810112000ZZ000003SILLMORERANDOMDATAFOLLOWSAFTERTHIS
Any ideas would be greatly appreciated!
Thanks, KSL.

No comments:

Post a Comment