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 04-20-2007, 09:14 PM   #1
coldy78
LQ Newbie
 
Registered: Apr 2007
Posts: 3

Rep: Reputation: 0
simple awk question getline


I am very new to awk and I hope somebody may be able to help me.

I have one large file which I would like to break up based on the contents of another smaller file

The small file small.txt has one field consisting of numbers 5 through to 200

These numbers correspond to a field in another file.

eg small.txt

5.5
7.8
10.3



large.txt


1 2 3 5.5
1 3 1 5.5
1 4 9 5.5
2 3 8 7.8
1 4 7 7.8
1 5 4 10.3
1 4 5 10.3
1 6 4 12.5
2 3 2 12.5
3 3 2 13.9

etc

So I only want the records in large file corresponding to records in small.txt


I tried

test.awk

getline cat < "small.txt"
$4 ~ /cat/ {print > "new.txt"}


awk -f test.awk large.txt

which does not give me what i was hopping for.

Any suggestions?
Thanks
 
Old 04-20-2007, 09:48 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Code:
awk '
   {
     while ( (getline line < "small.txt") > 0 ) {
        if (line == $4 ) { print }    
     }
   close("small.txt") 
   }
' "large.txt"
output:
Code:
#./test.sh
1 2 3 5.5
1 3 1 5.5
1 4 9 5.5
2 3 8 7.8
1 4 7 7.8
1 5 4 10.3
1 4 5 10.3
 
Old 04-20-2007, 11:18 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

Unless you are required to use awk, you can use grep:
Code:
#!/bin/sh

# @(#) s1       Demonstrate grep patterns from a file.

grep -f small.txt large.txt
Which produces:
Code:
% ./s1
1 2 3 5.5
1 3 1 5.5
1 4 9 5.5
2 3 8 7.8
1 4 7 7.8
1 5 4 10.3
1 4 5 10.3
cheers, makyo
 
Old 04-20-2007, 11:39 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

As a riff on ghostdog74's awk script -- this reads the strings only once into an associative array. Then as each data line is read, field 4 is checked to see if it has been used as an index string:
Code:
#!/bin/sh

# @(#) a1       Demonstrate reading strings into associative array.

awk '
BEGIN   { while ( ( getline line < "small.txt" ) > 0 ) {
                        seeking[line] = "true"
                        }
                }
                { if ( $4 in seeking ) {
                        print
                        }
                }
' large.txt
Producing:
Code:
% ./a1
1 2 3 5.5
1 3 1 5.5
1 4 9 5.5
2 3 8 7.8
1 4 7 7.8
1 5 4 10.3
1 4 5 10.3
cheers, makyo

( edit 1: typo )

Last edited by makyo; 04-21-2007 at 06:00 AM.
 
  


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
simple awk question mr_scary Linux - General 3 02-23-2007 06:37 PM
Simple question about sed or awk setianusa Programming 2 09-16-2005 03:57 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
simple c question (getline) aaa Programming 3 08-18-2004 03:02 PM
C++ | cin.getline question... Mega Man X Programming 7 01-08-2004 10:34 AM


All times are GMT -5. The time now is 06:30 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