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!

sequential number

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi all,
I have a file that looks like this:

2123 A C D
2123 A B C
2123 B C C
2124 B A D
2124 B D C
2124 B C C

I would like to add a field ( field2) with a sequential number depnding on value in field 1.
I would like to get this:
2123 1 A C D
2123 1 A B C
2123 1 B C C
2124 2 B A D
2124 2 B D C
2124 2 B C C

can any awk guru help?
Thanks
 
BEGIN {
num=0;
f1val="";
}

{
if ( $1 != f1val ) {
f1val=$1;
num++
}

$1 = $1 FS num;
print
} vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks Vlad,
But it looks like there is a problem somewhere with the script ( or with me?!), I am getting awk error messages.

N
 
can you be just a tiny bit more specific? ;)

how do you call a script?
what are the messages? vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Sorry Vlad,
It's working fine when I type it on the command line, although I get a 3 printd at the end, ?:
2123 1 A C D
2123 1 A B C
2123 1 B C C
2124 2 B A D
2124 2 B D C
2124 2 B C C
3

but when I saved your program (script)into a file and type awk -f script inputfile ,I got
the error message as follow:
syntax error near line 1
illegal satement near line 1
syntax error near line 2
illegal satement near line 2
syntax error near line 3
illegal satement near line 3
bailing out near line 4

I suspect that this might be due to my total incompetence! :)

Thansk again for your time

N
 
if on Solaris, use nawk insead of awk vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 

yes it was my incompetence, everything is working fine now.
Thanks again
N
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top