Если у вас все в порядке с -1 для всех значений, которые содержат только одно вхождение, вот пример программы.Вам нужно будет сделать копию в конце, так как вы не можете удалить исходный файл во время записи нового.
// Replace c:\temp\temp.txt with you original file.
// Replace c:\temp\temp2.txt with your temporary new file.
using (var r = new StreamReader(@"c:\temp\temp.txt"))
{
using (var w = new StreamWriter(@"c:\temp\temp2.txt"))
{
string line;
var counter = new Dictionary<string, int>();
// write header first, no changes necessary
if ((line = r.ReadLine()) != null)
{
w.WriteLine(line);
}
while ((line = r.ReadLine()) != null)
{
// assuming it is safe to split on a space
var values = line.Split(' ');
// if the value hasn't been encountered before, add it
if (!counter.ContainsKey(values[0]))
{
// start counter at 0
counter.Add(values[0], 0);
}
// increment the count as we hit each occurrence of the
// given key
counter[values[0]] = counter[values[0]] + 1;
// write out the original line, replacing the key with the
// format key-#
w.WriteLine(line.Replace(values[0],
string.Format("{0}-{1}",
values[0],
counter[values[0]])));
}
}
}
Пример ввода:
R.D. P.N. X Y Rot Pkg
L5 120910 64.770 98.425 180 SOP8
P4 120911 -69.850 98.425 180 SOIC12
L10 120911 -19.685 83.820 180 SOIC10
P4 120911 25.400 83.820 180 0603
L5 120910 62.484 98.425 180 SOP8
L5 120910 99.100 150.105 180 SOP8
Пример вывода(проверено):
R.D. P.N. X Y Rot Pkg
L5-1 120910 64.770 98.425 180 SOP8
P4-1 120911 -69.850 98.425 180 SOIC12
L10-1 120911 -19.685 83.820 180 SOIC10
P4-2 120911 25.400 83.820 180 0603
L5-2 120910 62.484 98.425 180 SOP8
L5-3 120910 99.100 150.105 180 SOP8
Если вы не хотите -1, вы всегда можете выполнить проверку перед записью, чтобы убедиться, что счетчик> 1.