Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Beginner: program files drive me crazy
Hello,

I really am a beginner with awk but I can't find help with my problem
in the awk tutorials I've looked at.
If someone could help me I'd be really grateful!

I have a text file called text.txt that looks a bit like this:

----------------------------
16,AND,3,4,5
32,BECAUSE, 16, 19, 23, 67, 104
27,BELLY,4,13,14,15

and so on, with each line in the format

<number>,<word>,<number>,<number>, ...
-----------------------------

This command-line awk script does what I want:

awk -F, '{if ($2 == "BECAUSE") print $1,$2}' text.txt

It prints: 32 BECAUSE

BUT how can I put this in a program file?

I have tried this in a file:

------------------------------------------
# awk program
FS = ","
{if ($2 == "BECAUSE") print $1,$2}
------------------------------------------

but running this file with awk -f just lists ALL the lines in the file.
And

------------------------------------------
# awk program
FS = ","
/BECAUSE/ {print $1,$2}
------------------------------------------

does the same!

I don't want all the lines in my file. I want the ones I want :)).
Can someone tell me what I am doing wrong?!?

Thanks,

Jan


Report this thread to moderator Post Follow-up to this message
Old Post
janbkr@yahoo.co.uk
05-13-05 08:58 PM


Re: Beginner: program files drive me crazy
BEGIN{ FS = "," }
{if ($2 == "BECAUSE") print $1,$2}


--
pop is Mark
If all is not lost, where is it?
--
<janbkr@yahoo.co.uk> wrote in message
news:1115983083.947403.57340@g44g2000cwa.googlegroups.com...
> Hello,
>
> I really am a beginner with awk but I can't find help with my problem
> in the awk tutorials I've looked at.
> If someone could help me I'd be really grateful!
>
> I have a text file called text.txt that looks a bit like this:
>
> ----------------------------
> 16,AND,3,4,5
> 32,BECAUSE, 16, 19, 23, 67, 104
> 27,BELLY,4,13,14,15
>
> and so on, with each line in the format
>
> <number>,<word>,<number>,<number>, ...
> -----------------------------
>
> This command-line awk script does what I want:
>
>    awk -F, '{if ($2 == "BECAUSE") print $1,$2}' text.txt
>
> It prints: 32 BECAUSE
>
> BUT how can I put this in a program file?
>
> I have tried this in a file:
>
> ------------------------------------------
> # awk program
> FS = ","
> {if ($2 == "BECAUSE") print $1,$2}
> ------------------------------------------
>
> but running this file with awk -f just lists ALL the lines in the file.
> And
>
> ------------------------------------------
> # awk program
> FS = ","
> /BECAUSE/ {print $1,$2}
> ------------------------------------------
>
> does the same!
>
> I don't want all the lines in my file. I want the ones I want :)).
> Can someone tell me what I am doing wrong?!?
>
> Thanks,
>
> Jan
>



Report this thread to moderator Post Follow-up to this message
Old Post
pop
05-13-05 08:58 PM


Re: Beginner: program files drive me crazy
Thank you!


Report this thread to moderator Post Follow-up to this message
Old Post
janbkr@yahoo.co.uk
05-13-05 08:58 PM


Re: Beginner: program files drive me crazy

janbkr@yahoo.co.uk wrote:

<snip>
> This command-line awk script does what I want:
>
>     awk -F, '{if ($2 == "BECAUSE") print $1,$2}' text.txt

ITYM:

awk -F, '$2 == "BECAUSE" {print $1,$2}' text.txt

<snip>
>
> ------------------------------------------
> # awk program
> FS = ","
> {if ($2 == "BECAUSE") print $1,$2}
> ------------------------------------------

BEGIN{FS=","}
$2 == "BECAUSE" {print $1,$2}

> but running this file with awk -f just lists ALL the lines in the file.

Yeah, it would. The FS="," would be seen as a true condition, invoking
the default action of printing $0.

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-13-05 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 10:08 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.