LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-27-2008, 04:27 AM   #1
Mauline
LQ Newbie
 
Registered: Mar 2005
Distribution: Redhat, Suse, Solaris (o:
Posts: 11

Rep: Reputation: 0
Question need help with awk-script (compare two lines)


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
 
Old 11-27-2008, 05:04 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Code:
awk '{ dept[$2] = dept[$2] "\n" $1; } END { for (var in dept) { print dept[var] > "file" var; } }' input
 
Old 11-27-2008, 05:12 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Why not simply this?
Code:
awk 'NR > 1 {print $1 >> ( "department" $2 ".txt" )}'
To build the file name just use string concatenation and protect it using parentheses. See here for more details.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare two files using Awk!! visitnag Linux - Newbie 3 09-15-2008 01:42 PM
AWK: compare two files haydar68 Programming 6 08-03-2008 12:20 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM


All times are GMT -5. The time now is 12:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration