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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming fields 1

Status
Not open for further replies.

mrr

Technical User
May 3, 2001
67
US
Could someone give me some help on restructuring a data file?
My input and desired output is as follows:

10 1 0000 0000
10 3 0005 0005
10 4 0010 0015
10 6 0020 0015
10 7 0025 0025
11 1 0040 0005
11 2 0045 0005
11 3 0050 0005
11 4 0055 0005
12 8 0010 0005
13 1 0020 0005
13 3 0030 0005
14 -10 0040 0005
14 -8 0050 0005
15 999 0060 0005

would become

10 1 0000 0000
10.1 3 0005 0005
10.1 4 0010 0015
10.2 6 0020 0015
10.2 7 0025 0025
11 1 0040 0005
11 2 0045 0005
11 3 0050 0005
11 4 0055 0005
12 8 0010 0005
13 1 0020 0005
13.1 3 0030 0005
14 -10 0040 0005
14.1 -8 0050 0005
15 999 0060 0005

What I am trying to accomplish is:
1. Check if current record is same or different than previous record on field 1.
If it is different write fields $1 - $4
If it is same on field 1 as previous record check the field 2 for gaps in number. The field 2 can be incrementing or decrementing.
If the field 2 gap is more than a pre-defined variable of 1 in this case, the gap would dictate a rename of the field 1 with a decimal 1-number suffix. I may have several gaps in field 2 that would require numerous field 1 extensions.

Thanks for any assistance.
 
This seems to do what you want
Code:
BEGIN { if (!inc) inc = 1}
{
  if ($1 != f1) {
    f1 = $1
    pf1 = $1
    if1 = 0
    f2 = $2
    print
  }
  else {
    df2 = f2 - $2
    if (df2 < 0) df2 = -df2
    if (df2 != inc) {
      if1++
      pf1 = f1 &quot;.&quot; if1
    }
    print pf1 &quot; &quot; $2 &quot; &quot; $3 &quot; &quot; $4
    f2 = $2
  }
}
Set variable inc to if you need an increment other than 1. I'll let you worry about lining up the columns if that's important. CaKiwi
 
CaKiwi,

Thanks, for the quick response and it works perfect.
mrr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top