Sep 10, 2008 #1 ooids Technical User May 2, 2001 21 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, 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
Sep 10, 2008 #2 feherke Programmer Aug 5, 2002 9,540 RO Hi Assuming that doubling the "2" lines is a typo ( you provided no rule for that ) : Code: awk 'l!=$1{l=$1;i=0}{$2=++i}1' /input/file Feherke. http://rootshell.be/~feherke/ Upvote 0 Downvote
Hi Assuming that doubling the "2" lines is a typo ( you provided no rule for that ) : Code: awk 'l!=$1{l=$1;i=0}{$2=++i}1' /input/file Feherke. http://rootshell.be/~feherke/
Sep 10, 2008 Thread starter #3 ooids Technical User May 2, 2001 21 US 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! Upvote 0 Downvote
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!
Sep 10, 2008 #4 PHV MIS Nov 8, 2002 53,708 FR One way: awk 'function p(){if{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 Upvote 0 Downvote
One way: awk 'function p(){if{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