Fedora Linux Support Community & Resources Center
  #1  
Old 24th April 2006, 03:07 PM
Velvohm Offline
Registered User
 
Join Date: Feb 2006
Location: Toronto
Posts: 29
awk is becoming real 'awk'ward

Hello,

I am having some trouble with awk or gawk for that matter. I want to write a script either in the command line or in a separate file that reads two pre-made files, say F1 and F2. The task is actually quite simple. It reads user -speciied columns/fields from F1 and F2 and prints them out as one record to another file called say F3. For instance, I want to read $1, $2 from F1 and $1 from F2. F3 should contain 3 columns: $1, $2 from F1 and $1 from F2. The problem is I cannot seem to make awk recognize the different fields in F1 and F2. Can anyone help me? thank you and please be specific as I am a novice awk user, as u probably can tell by this simple problem.

Cheers
velvohmmm
Reply With Quote
  #2  
Old 27th April 2006, 12:45 PM
VictorienSardou Offline
Registered User
 
Join Date: Feb 2006
Posts: 628
This script makes the ASSUMPTION that f1.txt and f2.txt have the same number of records ie the line count is the same.
Failure to maintain this will cause undefined output.

[root@lima awk]# cat prog.awk
BEGIN {FS=",";ORS=""}
{
if (FILENAME==ARGV[1]) {
getline <ARGV[1]
print $2,$3,""
getline <ARGV[2]
print $2,"\n"
} else {
exit
}
}
[root@lima awk]# cat f1.txt
q,w,e,r
a,s,d,f
z,x,c,v
[root@lima awk]# cat f2.txt
r,t,y
f,g,h
v,b,n
[root@lima awk]# awk -f prog.awk f1.txt f2.txt > f3.txt
[root@lima awk]# cat f3.txt
w e t
s d g
x c b
[root@lima awk]#
Reply With Quote
  #3  
Old 28th April 2006, 08:56 PM
Velvohm Offline
Registered User
 
Join Date: Feb 2006
Location: Toronto
Posts: 29
Thank you for this detailed explanation. Although, I solved the problem, I was wondering how to do with command line arguments and you seemed to have hit the hail dead on. Take care

Cheers
velvohm
Reply With Quote
  #4  
Old 2nd May 2006, 09:45 PM
Velvohm Offline
Registered User
 
Join Date: Feb 2006
Location: Toronto
Posts: 29
Sorry to bother you but do u know how to make the above program as an executable. I have tried adding just the lines
#!/bin/awk -f to make an awk executable or just the lines #!/bin/sh awk ' ......prog.awk.... ' to the file prog.awk to make a shell executable but it keeps giving me annoying errors.

Thanks

Velvohm
Reply With Quote
  #5  
Old 3rd May 2006, 04:25 PM
VictorienSardou Offline
Registered User
 
Join Date: Feb 2006
Posts: 628
glad to help, i am happy you liked the script.
not sure what you are doing but this works for me.

[root@lima awk]# chmod +x prog.awk
[root@lima awk]# cat prog.awk
#!/usr/bin/awk -f
BEGIN {FS=",";ORS=""}
{
if (FILENAME==ARGV[1]) {
getline <ARGV[1]
print $2,$3,""
getline <ARGV[2]
print $2,"\n"
} else {
exit
}
}
[root@lima awk]# ./prog.awk f1.txt f2.txt
w e t
s d g
x c b
[root@lima awk]#
Reply With Quote
  #6  
Old 4th May 2006, 08:04 PM
Velvohm Offline
Registered User
 
Join Date: Feb 2006
Location: Toronto
Posts: 29
Thanx for prompt reply. I had to make some changes because your code was working assuming both files were of same size. But finally got it working.

Cheers
Mowly

Oh because you were curious, I need this code for network evaluation using NS-2. that is all
Reply With Quote
Reply

Tags
awk, awkward, real

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Real audio and Real Player chejose Using Fedora 31 6th February 2007 10:35 PM
Real Dave001 Using Fedora 3 5th February 2007 05:16 AM
real player lavender Installation, Upgrades and Live Media 1 2nd February 2007 10:19 PM
Real player anjan25 Using Fedora 10 13th October 2006 12:08 AM
The real speed.......... blinksilver Using Fedora 10 22nd March 2005 10:34 AM


Current GMT-time: 06:16 (Friday, 31-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Kanaya Photos on Instagram - Sakai - Taguig Instagram Photos