awk beginners question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk beginners question
# 1  
Old 11-16-2010
awk beginners question

hi,

i start using awk and have a very basic problem. here's my code:

Code:
#! /usr/bin/awk -f
# 2010, scz
#


{
	$1 == "test" { print $2 } 
}

this works on the command line but not as "program" - what is the difference between awk programs on the command line and executing awk commands from a file?

rgds,
Sven
# 2  
Old 11-16-2010
Are you sure it works from the command line?

Code:
 $ awk '{ $1 == "test" { print $2 } }' file1
 Syntax Error The source line is 1.
 The error context is
                { $1 == "test" >>>  { <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
 Syntax Error The source line is 1.

Try removing the outer braces:

Code:
#! /usr/bin/awk -f
# 2010, scz
#
    $1 == "test" { print $2 }

This User Gave Thanks to Scott For This Post:
# 3  
Old 11-16-2010
You have a syntax error becuase you are embedding a pattern:action pair inside another.
Just remove the enclosing { }...
Code:
$ cat script1.awk
#! /usr/bin/awk -f
# 2010, scz
#

        $1 == "test" { print $2 }


$ chmod 775 script1.awk

$ echo test 123 > file1

$ ./script1.awk file1
123

$

# 4  
Old 11-16-2010
thanks scott,

you are right - i've left out the braces on the command line. the script file also worked after removing them.

i was using braces like in c ... my 1st lesson!

Last edited by svencz; 11-16-2010 at 04:37 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX ebook for beginners

hi all, Can you suggest me a ebook for unix beginners. I am new to unix. (2 Replies)
Discussion started by: rajasingam
2 Replies

2. UNIX for Dummies Questions & Answers

UNIX for beginners

i'm just a beginner in unix environment- please help which book to read and which os to use!!! :confused: seriously i've no idea what is unix or how much capable it is!! (1 Reply)
Discussion started by: gaurav singh
1 Replies

3. Shell Programming and Scripting

Shell program for beginners

Hey, i hope someone can help me with this program. I need to write a program in shell which will return how many times and how much time have users been logged in system between two dates. We give time as 2 dates as arguments in command line. Example: $ nameofprogram 27/04 06/05 ... (1 Reply)
Discussion started by: Exander
1 Replies

4. Programming

Beginners question about fork

Hi everyone: I'm developing a dynamic library for notifications, this library is used for a daemon that i've programmed, when something goes wrong the library should send an email to an administrator, but since sending an email is a non-vital process then it can fail (it should work as an... (4 Replies)
Discussion started by: edgarvm
4 Replies

5. UNIX Desktop Questions & Answers

UNIX for beginners

I am new to non Windows operating systems. Does anyone have advice on which UNIX OS vendor would be good for learning purposes. I was looking for a version of UNIX that runs on the Intel platform. Do you have any recommendations on where to purchase the software? Thank you. (14 Replies)
Discussion started by: jmy113437
14 Replies

6. Shell Programming and Scripting

awk - Beginners Question

I have my inputfile in the following format : From:sdhfhg dsfhsdjfjdsfh dsfjdjshjsd djfhsdjfjsdhjds Error Description <aa.aa.aa.aa.aa.aa> From:ksljfsdhfjh djfdsjkf sdjwoquk dsfsdfj Error Description <dd.dd.dd.dd.dd> I want to read the lines from tag 'From:' thrul <aa.aa.aa.aa.aa.aa>... (1 Reply)
Discussion started by: Amruta Pitkar
1 Replies

7. UNIX Desktop Questions & Answers

unix course for beginners

does anyone know of a course for unix beginners (1 Reply)
Discussion started by: moose
1 Replies

8. Programming

X-programming for beginners

Good morning. Thanks for the very valuable hard-to-find information I get from you guys. Can anybody give any suggested websites or references for anyone who wants to begin learning on programming applications in X? Thanks to anyone in advance... (1 Reply)
Discussion started by: jfsuminist
1 Replies
Login or Register to Ask a Question