awk syntax doubt - trailing 1 value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk syntax doubt - trailing 1 value
# 1  
Old 10-26-2008
awk syntax doubt - trailing 1 value

In the below awk syntax, what does the value '1' signify?

awk '{....}1' file

some eg:
Code:
awk '{gsub(/[^[:cntrl:]]/,"")}1'
awk 'BEGIN{ORS=""}1'
awk '{ORS=(!(NR%5)?"":"\n")}1'

# 2  
Old 10-26-2008
awk evaluates the 1 as true and the prints the entire line by default, including a newline.
Code:
echo 'foo' | awk '/foo/{print}1'

Take a look here: http://sunsite.ualberta.ca/Documenta...l#SEC_Contents
# 3  
Old 10-28-2008
Quote:
Originally Posted by danmero
awk evaluates the 1 as true and the prints the entire line by default, including a newline.
Code:
echo 'foo' | awk '/foo/{print}1'

Take a look here: The GNU Awk User's Guide: Table of Contents
But, what does 1 serve here or what is the role/purpose of 1?
Anyway, the default action of awk is to print the record right, even if the 'print' statement is missing?

Last edited by royalibrahim; 10-28-2008 at 06:34 AM..
# 4  
Old 10-28-2008
Quote:
Originally Posted by royalibrahim
But, what does 1 serve here or what is the role/purpose of 1?
Anyway, the default action of awk is to print the record right, even if the 'print' statement is missing?
Don't take everything for granted.
Code:
$ echo 'foo' | awk '/foo/'            #default action
foo

$ echo 'foo' | awk '/foo/{print}'     #defined action
foo
$ echo 'foo' | awk '/foo/{$1="bar"}'  #defined action

$ echo 'foo' | awk '/foo/{print}1'    #defined action and default action
foo
foo

$ echo 'foo' | awk '/foo/{$1="bar"}1'
bar

# 5  
Old 10-29-2008
Thanks danmero, I got the essence !! hat's off
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doubt in awk

Hi, I got a below requirement from this forum, but the solution provided was not clear. Below is the requirement Input file A 1 Z A 1 ZZ B 2 Y B 2 AA Required output B Y|AA A Z|ZZ (5 Replies)
Discussion started by: stew
5 Replies

2. Shell Programming and Scripting

awk trailing character removal

Hi, The command - id | awk '{print $1}' - returns the following: uid=9028(luke) What do I need to further that awk so that I only have "luke", I want to set this as a variable. Thanks in advance, Lukas. P.S: I've come up with: USER1=`id | awk F'(' '{print $2}' | awk -F')' '{print... (4 Replies)
Discussion started by: luke222010
4 Replies

3. UNIX for Dummies Questions & Answers

a doubt in awk

instead of writing print command in awk, i saw in some posts that we can simply write a number before we end the awk command and it will print the file. As given below: $awk '{some manipulation; print}' filename $awk '{some manipulation}1' filename I also tried replacing the... (2 Replies)
Discussion started by: PranavEcstasy
2 Replies

4. Shell Programming and Scripting

using awk to strip out decimal point and trailing integers

Hey guys, I've got an awk string that does a simple calculation which works well. However, I'd really like to be able to strip the decimal point and the trailing ints from it - any ideas on how I'd change the awk string to be able to do that ? I've changed it countless times and each time it... (14 Replies)
Discussion started by: jimbob01
14 Replies

5. Programming

Linux kernel code syntax doubt

I have found some definitions as below, can anyone explain me what it means and why it is being used this way please. asmlinkage void __sched schedule(void) (4 Replies)
Discussion started by: kumaran_5555
4 Replies

6. Shell Programming and Scripting

How to delete ending/trailing spaces using awk,sed,perl?

How to delete ending/trailing spaces using awk,sed,perl? Input:(each line has extra spaces at the end) 3456 565 3 7 35 878 Expected output: 3456 565 3 7 35 878 (5 Replies)
Discussion started by: cola
5 Replies

7. Shell Programming and Scripting

Awk doubt

I have a file sample.txt with the following contents: the following gives output as awk 'NF{s=$0; print s}' sample.txt but, awk 'NF{s=$0}{print s}' sample.txtgives output as why this difference, can someone explain me? (6 Replies)
Discussion started by: royalibrahim
6 Replies

8. Shell Programming and Scripting

doubt on awk

I have executed the below command: find . -name "Ks*" -type f -exec ls -ltr {} \; | awk '{printf("%ld %s %d %s \n",$5,$6,$7,$8,$9)}' and here is the output: 1282 Oct 7 2004 51590 Jul 10 2006 921 Oct 7 2004 1389 Jun 4 2003 1037 May 19 2004 334 Mar 24 2004 672 Jul 8 2003 977... (6 Replies)
Discussion started by: venkatesht
6 Replies

9. UNIX for Dummies Questions & Answers

doubt about awk

i have a file like this: awk.lst smith : sales : 1200 : 2 jones:it:25000 : 2 roger : it : 1500 : 2 ravi | acct | 15000 i have 3 doubts 1) when i say awk -F ":" '$2 ~ /'it'/ {print $0}' awk.lst i am not able to get jones in the ouput , is it because of space issue? 2)how to... (2 Replies)
Discussion started by: soujanya_srk
2 Replies

10. UNIX for Dummies Questions & Answers

awk doubt

I'm having a file with 5 fields. I want to sort that file according to one field no 3. How shall I do using awk programming. Any input appreciatable. regards, vadivel. (7 Replies)
Discussion started by: vadivel
7 Replies
Login or Register to Ask a Question