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!

Changing values in a file

Status
Not open for further replies.

chalcopyrites

Technical User
Mar 11, 2004
12
GB
Hello,

I hope you can help me!

I have a file like this:

545235.45 546547.36 7551.33 78854 0
465745.54 857435.35 7445.36 78854 0
774546.32 548745.35 4552.25 78854 0
465433.32 457657.25 7865.36 78854 0
754654.32 454654.25 7845.37 78854 0
755445.32 454544.85 7854.35 78854 1
744556.34 755442.36 7854.37 78854 1
787545.32 785566.32 7854.64 78854 1
785845.38 784545.14 7855.39 78854 1

I would like to change the last column of my file so it looks like this:

545235.45 546547.36 7551.33 78854 1
465745.54 857435.35 7445.36 78854 2
774546.32 548745.35 4552.25 78854 2
465433.32 457657.25 7865.36 78854 2
754654.32 454654.25 7845.37 78854 3
755445.32 454544.85 7854.35 78854 1
744556.34 755442.36 7854.37 78854 2
787545.32 785566.32 7854.64 78854 2
785845.38 784545.14 7855.39 78854 3

ie instead of a constant number in the last column for each group of records there is a 1 for the first line of the group, 3 for the last and 2 for the lines in between. The value in the end column increments by 1 until it reaches 999 when it starts at 0 again and there isn't a fixed number of records for each group.

Many thanks!
 
Hi

As ugly as possible :
Code:
{
  if ("" group!=$5) {
    if (line) {
      print line,3
      line=""
    }
    group=$5
    $5=1
    print $0
  } else {
    if (line) print line,2
    NF--
    line=$0
  }
}
END {
  print line,3
}

Feherke.
 
Hi Feherke,

Thanks for your response. It didn't give quite the output I was looking for, it came out like this:

545235.45 546547.36 7551.33 78854 1
465745.54 857435.35 7445.36 78854 0 2
774546.32 548745.35 4552.25 78854 0 2
465433.32 457657.25 7865.36 78854 0 2
754654.32 454654.25 7845.37 78854 0 3
755445.32 454544.85 7854.35 78854 1
744556.34 755442.36 7854.37 78854 1 2
787545.32 785566.32 7854.64 78854 1 2
785845.38 784545.14 7855.39 78854 1 3

so I modified your script a little

{
if ("" group!=$5) {
if (line) {
print line,3
line=""
}
group=$5
$5=1
line=$0
print line,1
} else {
if (line) print line,2
NF--
line=$0
}
}
END {
print line,3
}

and my output is now

545235.45 546547.36 7551.33 78854 0 1
545235.45 546547.36 7551.33 78854 0 2
465745.54 857435.35 7445.36 78854 0 2
774546.32 548745.35 4552.25 78854 0 2
465433.32 457657.25 7865.36 78854 0 2
754654.32 454654.25 7845.37 78854 0 3
755445.32 454544.85 7854.35 78854 1 1
755445.32 454544.85 7854.35 78854 1 2
744556.34 755442.36 7854.37 78854 1 2
787545.32 785566.32 7854.64 78854 1 2
785845.38 784545.14 7855.39 78854 1 3

which is what I need

Thanks again for your help!

Chalcopyrites
 
Hi

Interesting. I use [tt]gawk[/tt] 3.1.1 and the result was exactly what you specified. ( Verified with [tt]diff[/tt]. ) Probably there are some incompatibilities.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top