awk miscellaneous doubts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk miscellaneous doubts
# 1  
Old 08-22-2008
awk miscellaneous doubts

In this following command:
awk 'BEGIN{ORS=""}1'

what does '1' signifies that comes after closing curly brace '}' of awk? I guess, it does not mean 'first occurrence' because I verified that.

And, pls tell me how to override or suppress awk's field variables like $1, $2.. by positional parameters of the shell inside the script?

Thanks
Ibrahim

Last edited by royalibrahim; 08-22-2008 at 11:30 AM..
# 2  
Old 08-22-2008
as far as i know..
if you include 1 it prints whatever in its pattern space..
pattern space is the place where it keeps the line or record its read..

not sure but... not able to recall..
# 3  
Old 08-22-2008
An AWK program consists of a sequence of pattern-action statements (and optional function definitions)
Code:
pattern   { action statements }

The pattern is evaluated and if the result is true the action statements are executed.
If no pattern is not specified, the default value is true so all records are selected and the actions are allways executed.
For example the following code prints all input records converted to lowercase :
Code:
 { print tolower($0) }

If the action statements are not specified the dafault is print the input record.
For example, the following code prints all input records containing the string "OK" :
Code:
$0 ~ /OK/

In your code there are two sequences of pattern-action
Code:
BEGIN {      # Special pattern, allways true before anay of the input is read
   ORS=""    #  Set the Output Record Separator to null string
}            #
1            # Pattern value 1 is evaluated to true, so all records are selected
             #   The defalt action is 'print $0'
' inputfile

Your awk command merge all records (all records are printed without record separator because ORS="")

Jean-Pierre.
# 4  
Old 08-22-2008
I still could not understand about the 'pattern value' or 'pattern space' concept. What happens if we change 1 as 2 in the above awk command?
# 5  
Old 08-22-2008
Quote:
Originally Posted by royalibrahim
I still could not understand about the 'pattern value' or 'pattern space' concept. What happens if we change 1 as 2 in the above awk command?

it works only for 1 not for 2 or more..
# 6  
Old 08-22-2008
Quote:
Originally Posted by royalibrahim
I still could not understand about the 'pattern value' or 'pattern space' concept. What happens if we change 1 as 2 in the above awk command?
The pattern is an expression that is evaluated for each record. If the result is true the action statements are executed.
If the result of the evaluation is a string or a number, the empty string "" and the number zero are considered to be false, all other values are true.
So the patterns 1 and 2 gives the same result that is true.

Jean-Pierre.
# 7  
Old 11-10-2008
If I want to remove the second column as well as the delimiter (which is comma ',' here) following it, I am using this script.

Code:
echo '1,2,3' | awk 'BEGIN{FS=OFS=","} {$2=""}1'| awk '{sub(",,",",")}1'

output: 1,3

Is it possible to have a single awk command in the above instead of spawing 2 awks to accomplish this task?

Also, pls someone clarify me, which is the proper syntax of awk's sub()? sub(/<pattern>/,"<replacement>") (or) sub(<pattern>,"<replacement>")

Last edited by royalibrahim; 11-10-2008 at 03:11 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Doubts About awk, and Gawk

well i have some doubts about the use of this commands: my first doubt is to know if there is a way to execute a awk program from a file? (now i do copy paste, i copy the script of a notepad on the terminal and then i press enter, but i want to put this scripts in some folder and execute them)... (3 Replies)
Discussion started by: matius_88
3 Replies

2. HP-UX

Some doubts about resizing fs's in HP-UX

Hello, I'm new to HP-UX and I'm not sure about some concepts related to resizing fs's under this OS. First of all I'm only asking about resizing ONLINE, it means, without having to umount the fs nor rebooting, etc. Q1. I've read that in order to resize a fs online there are 2 requirements:... (3 Replies)
Discussion started by: asanchez
3 Replies

3. Shell Programming and Scripting

Awk miscellaneous doubts

Hi, I am having the following doubts on awk. Please clarify for me. a) What does it mean? awk '$1=$1' and how does it change if I append FS="" to the above code? b) What if I use awk -vFS="\n" (i.e) setting (input) field separator to newline char, then what will be the value of $0,... (6 Replies)
Discussion started by: royalibrahim
6 Replies

4. Shell Programming and Scripting

Miscellaneous operators

Hi everyone, I read some shell script code,then I have some issue. the following code. let "t1 = ((5 + 3, 7 - 1, 15 - 4))" echo "t1 = $t1" t1=11 Here t1 is set to the result of the last operation.why? (3 Replies)
Discussion started by: luoluo
3 Replies

5. Shell Programming and Scripting

awk, sed and a shell doubts

Hi, I have 3 doubts which I posted it here. Doubt 1: I have a file containing data: 22 -73 89 10 99 21 15 -77 23 63 -80 91 -22 65 28 97 I am trying to print the fields in the reverse order and replace every field by its absolute (positive) value (ie.) I am looking for output: 10... (8 Replies)
Discussion started by: royalibrahim
8 Replies

6. UNIX for Dummies Questions & Answers

Unix doubts

Hi All, 1. how and who calls .profile when you login 2. what is PPID and what means by PPID = 0 Thanks in Advance (2 Replies)
Discussion started by: ravi.sadani19
2 Replies

7. UNIX for Dummies Questions & Answers

Doubts on FIFO

Hi , I m beginner for Unix and i want to use FIFO in my 2 Scripts . I want 1 script to read data from FIFO and other will write into FIFO. Despite reading so many articles/posts i am still unable sunchronize my scripts. My doubts are 1> Do We require both scripts as daemons to use... (0 Replies)
Discussion started by: Akshay
0 Replies

8. Shell Programming and Scripting

doubts in crontab

Hi All, I am tring to set a shedule for cron to execute my script in every 15 min, n want a mail alert on my mail id if cron fails to get data as input for my script, how can i set that? Thanks in advance Subin (1 Reply)
Discussion started by: subin_bala
1 Replies

9. UNIX for Dummies Questions & Answers

unix doubts

Write unix command to list or view auto links (0 Replies)
Discussion started by: ashishshah.engg
0 Replies
Login or Register to Ask a Question