behaviour of awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers behaviour of awk
# 1  
Old 02-26-2012
behaviour of awk

Can someone explain how the below awk simulates cat?
Code:
pandeeswaran@ubuntu:~$ awk '1' file
PSAPSR3 3722000 91989.25 2 98
PSAPSR7 1562000 77000.1875 5 95
PSAPUNDO 92000 4087.5625 4 96
pandeeswaran@ubuntu:~$ awk '2' file
PSAPSR3 3722000 91989.25 2 98
PSAPSR7 1562000 77000.1875 5 95
PSAPUNDO 92000 4087.5625 4 96
pandeeswaran@ubuntu:~$ awk '0' file
pandeeswaran@ubuntu:~$ awk '3' file
PSAPSR3 3722000 91989.25 2 98
PSAPSR7 1562000 77000.1875 5 95
PSAPUNDO 92000 4087.5625 4 96

So, any non zero value covered by '' will give the result similar to cat?
How it works?
Thanks
# 2  
Old 02-26-2012
Refer to the awk man page:

Code:
       A pattern-action statement has the form

              pattern { action }

       A missing { action } means print the line; a missing pattern always matches.  Pattern-action  statements
       are separated by newlines or semicolons.

"1" is the pattern which evaluates to true. As no "action" is specified, the line is printed.

It's equivalent to:
Code:
1 == 1 { print }

or
Code:
1 { print }

or just
Code:
{ print }

This User Gave Thanks to Scott For This Post:
# 3  
Old 02-26-2012
Thanks scott..but i am curious why the below is not working?
Code:
awk '-1==-1' file

# 4  
Old 02-26-2012
That's an interesting question!

awk seems to treat the first - as an option. Using awk ' -1 == -1^ (see the space before the first -), or awk -- '-1 == -1^ gets around that.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Strange behaviour in AIX

Can someone please explain the strange behaviour.. I was just trying a few things to learn awk.. in the below code when I start the braces in the same line, the output is as expected, when I start at next line, output is displayed twice. Please see the file, code I tried and output below. ... (2 Replies)
Discussion started by: Kulasekar
2 Replies

2. Shell Programming and Scripting

Strange behaviour of arrays in awk

Imagine 2 files f1 f2: file1_l1_c1 code_to_find file1_l1_c3 file1_l2_c1 file1_code2 file1_l2_c3 file1_l3_c1 file1_code3 file1_l3_c3 file2_l1_c1 file2_l1_c2 code_to_find file2_l2_c1 file2_l2_c2 file2_code5 file2_l3_c1 file2_l3_c2 file2_code3 Say we want to print lines from f2 having... (5 Replies)
Discussion started by: ripat
5 Replies

3. UNIX for Dummies Questions & Answers

Strange behaviour when output to terminal vs file (awk)

Hi all ! I noticed something very weird. I have a large pipe delimited file (20 fields/3,000 records) that looks like that: AAA|BBB|11111|22222|...|($NF of record 1) CCC|DDD|33333|44444|...|($NF of record 2) CCC|DDD|55555|66666|...|($NF of record 3) For the lines with same 1st and 2nd... (3 Replies)
Discussion started by: beca123456
3 Replies

4. Programming

different behaviour in fg and bg

fg = foreground bg = background I have a cobol program that I start with a very simple script. The script is not at fault as it has not changed and the program worked in fg and bg before. I have altered the logging in the program and moved my cursor declare to working storage. The program runs... (6 Replies)
Discussion started by: Bruble
6 Replies

5. Ubuntu

Weird rm behaviour

I am little bit confused by the behaviour of rm in Ubuntu. It seems that as a regular user I can delete files owned by another user even when the permissions are set to 644. Here is an example: cjohnson@carbon:~/test$ sudo touch testfile cjohnson@carbon:~/test$ ls -al total 8 drwxr-xr-x... (2 Replies)
Discussion started by: ccj4467
2 Replies

6. Shell Programming and Scripting

Don't understand AWK asort behaviour

Hello, I have the following script : BEGIN { print "1 ***"; split("abc",T,""); T="e"; T="z"; T="y"; for (i in T) printf("%i:%s ",i,T); print ""; for (i=1; i<=length(T); i++) printf(T); print "" print "2 ***"; asort(T,U); for (i in U) printf("%i:%s ",i,U); ... (3 Replies)
Discussion started by: jgilot
3 Replies

7. Shell Programming and Scripting

cp -R behaviour

i 've noticed the following difference between freebsd cp and gnu cp from the freebsd cp man page: -R ... If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. ... on gnu cp from the man pagewhile on gnu cp manpage: ‘-r'... (2 Replies)
Discussion started by: aegis
2 Replies

8. Programming

Can some 1 explain why this behaviour

#include <iostream> using namespace std; int main() { const int l_test = 999999999; int *l_ptr = (int*) &l_test; cout<<"Constant Addr:"<<&l_test<<" Value:"<<l_test<<endl; cout<<"Pointer Addr:"<<l_ptr<<" Value:"<<*l_ptr<<endl; *l_ptr = 888888888; // Manipulating... (2 Replies)
Discussion started by: helpmenow
2 Replies

9. Programming

Behaviour of default

Hi, If I have the following code : int i=2; switch(i) { case 1 : {};break; default : { printf("d"); };break; case 2 : { printf("2"); };break; } what will be the output? I had an understanding that case statements are taken sequentially so default will be executed. But... (5 Replies)
Discussion started by: soorajmu
5 Replies
Login or Register to Ask a Question