For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2005 > Beginner: program files drive me crazy









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Beginner: program files drive me crazy
janbkr@yahoo.co.uk

2005-05-13, 3:58 pm

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

pop

2005-05-13, 3:58 pm

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
>



janbkr@yahoo.co.uk

2005-05-13, 3:58 pm

Thank you!

Ed Morton

2005-05-13, 3:58 pm



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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com