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

Simple counter

Status
Not open for further replies.

ooids

Technical User
Joined
May 2, 2001
Messages
21
Location
US
Hi,

I have a simple input file:
1
1
1
1
2
2
2
2
3
3
3

I need to create an additional column in the file which is based on the first columns numbers:

1 1
1 2
1 2
1 3
2 1
2 2
2 2
2 3
3 1
3 2
3 3

etc

Any ideas would be great!

Many Thanks


 
Hi Feherke,

No typos.
The first new occurrence of a number = 1
Subsequent occurrences of the same number = 2
The last occurrence of the number = 3

Hope that makes sense - what I am struggling with is being able to compare the current line with the previous one so I know when the number changes

Thanks!
 
One way:
awk 'function p(){if(n){print l"\t1";for(i=2;i<n;++i)print l"\t2";if(n>1)print l"\t3"}}l!=$1{p();n=0;l=$1}{++n}END{p()}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Many thanks!
 
Status
Not open for further replies.

Similar threads

Replies
1
Views
298
  • Locked
  • Question Question
Replies
1
Views
234
Replies
20
Views
809
  • Locked
  • Question Question
Replies
9
Views
321

Part and Inventory Search

Sponsor

Back
Top