Hi all,
I'm having problems with a "&%=?!""-awk script
and hopefully somebody of you pros can help me:
Having a file with the following content:
name department
a 1
b 1
c 2
d 3
e 3
f 4
g 5
a 5
...
I need to generate a file for every deparment containing all names which belong to this department.
i.e. department1.txt will contain
a
b
department2.txt will contain
c
etc.
But I'm not so far yet ... here's my code:
awk '{
cline = $0
dept = $2
print "dept: " dept # debug
if ((getline nline) > 0 ) {
nextdept = substr($2, nline)
print "next dept: " nextdept #debug
if (cline == nline) {
print "\n next dept is the same"
print $2 >> "dept1.txt"
} else {
print "next dept differs"
print $2 >> "dept2.txt"
}
} else
print "EOF"
}' input.txt
And here's the output:
dept: 1
next dept: 1
next dept differs
dept: 2
next dept: 2
next dept differs
dept: 3
next dept: 3
next dept differs
dept: 5
EOF
So it's not doing the if-clause correctly.
Do you know how I can dynamically change the filename in awk?
Like
print dept > dept_$dept.txt?
Thanks!
Romy