Unable to understand awk script.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unable to understand awk script.
# 1  
Old 04-04-2014
Unable to understand awk script.

Hi

below is the samll awk script which i am not able to understand properly.

here a bit confusion in 2nd line for me
1. why
Code:
*\

is not used before
Code:
.[0-9][0-9]

in second line as it was used in first line
Code:
*\$[1-9][0-9]*\.[0-9][0-9] */

2. why always \ is used in every first line

3. is there any specific meaning of
Code:
*\

and
Code:
*/


awk script mentioned below
Code:
#!/bin/sh awk ' / *\$[1-9][0-9]*\.[0-9][0-9] */ { print $1,$2,$3,"*"; } / *\$0\.[0-9][0-9] */ { print ; }
' fruit_prices.txt

below is input file
Code:
Fruit Quantity Banana 100 Peach 65 Kiwi 22 Pineapple 35
Apple 78


Regards,
Scriptor

Last edited by scriptor; 04-04-2014 at 03:10 AM.. Reason: typo error
# 2  
Old 04-04-2014
These are regular expressions.
* means zero or more spaces
\. and \$ mean a literal dot and a literal dollar sign
/ .... / these two slashes are not part of a regular expression, but rather demarcate it and are part of the awk syntax..


So for example the first regular expression used is
Code:
 *\$[1-9][0-9]*\.[0-9][0-9] *

With first character being a space..
# 3  
Old 04-04-2014
Hi Scrutinizer,

not able understand the use of of
Code:
*

in below script.
Code:
 
#!/bin/sh
awk '
/ *\$[1-9][0-9]*\.[0-9][0-9] */ { print $1,$2,$3,"*"; }
/ *\$0\.[0-9][0-9] */ { print ; }

Code:
' fruit_prices.txt



as i dont see any difference if i removed the
Code:
*

from script. the o/p remain same.
script after removing
Code:
*

please see below

Code:
awk '
/ \$[1-9][0-9]*\.[0-9][0-9] / { print $1,$2,$3,"*"; }
/ \$0\.[0-9][0-9] / { print ; }' fruits1

regards,
scriptor

# 4  
Old 04-04-2014
There may be no difference with your particular input file.

In the original regex there should be zero or more spaces around the number. So that is a strange regex, as it is equivalent to
Code:
/\$[1-9][0-9]*\.[0-9][0-9]/

and
Code:
/\$0\.[0-9][0-9]/



Your revisions / \$[1-9][0-9]*\.[0-9][0-9] / / \$0\.[0-9][0-9] / specify that there should be a space before and after the number.

I see no relationship with your input file, however. What is it that you intend to accomplish?

Last edited by Scrutinizer; 04-04-2014 at 06:10 AM..
# 5  
Old 04-04-2014
Hi Scrutinizer,

actually i was studing about the awk the example which i share you in my 1st post was given in that tutorial. where i anot able to understatnd it completely.

can you please explain me with example so that i can understand the significance of the
Code:
*

in such type of example.

thx in advance

Scriptor

Last edited by scriptor; 04-04-2014 at 06:24 AM.. Reason: typo correction
# 6  
Old 04-04-2014
* means a match for zero or more occurrences of the preceding single character or sub-expression. Other than that I am unsure what you want to know..
# 7  
Old 04-04-2014
Hi Scrutinizer,

as you said the
Code:
*

means match for zero or more occurrence. that part i understand.

but when i remove one
Code:
*

(which is present in middle) in first line the o/p got changed . as compared to the one without making any change. can you please explain why ?

see below changed code

Code:
 
$ awk '
> / *\$[1-9][0-9] \.[0-9][0-9] */ { print $1,$2,$3,"*"; }
> / *\$0\.[0-9][0-9] */ { print ; }
> ' fruits1
Banana $0.89 100
Peach $0.79 65
Apple $0.99 78
 
o/p
 
Banana $0.89 100
Peach $0.79 65
Apple $0.99 78


here i feel the working of each astric is different. can you please comment on this too


below the content given in tutorial

Code:
Taking Pattern-Specific Actions Say that you want to highlight those fruits that cost more than a dollar by putting a * at the end of the line for those fruits. This means that you need to perform different actions depending on the pattern that was matched for the price. Start with the following script: #!/bin/sh awk ' / *\$[1-9][0-9]*\.[0-9][0-9] */ { print $1,$2,$3,"*"; } / *\$0\.[0-9][0-9] */ { print ; }
' fruit_prices.txt
The output looks like the following: Banana $0.89 100 Peach $0.79 65 Kiwi $1.50 22 * Pineapple $1.29 35 *
Apple $0.99 78

thx in advance.

Scriptor

Last edited by scriptor; 04-04-2014 at 07:26 AM.. Reason: missed to add one point
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

unable to understand the output of TRUSS command

Hi, I am trying to set ulimit for soft stack unlimited, but this is not taking effect, after tracing the ulimit -a unlimited command, the below output was generated, which i am unable to comprehend. Could any one help me with this? prcbap1-r10prod: truss -d ulimit -s unlimited Tue Dec 30... (2 Replies)
Discussion started by: NasirAbbasi
2 Replies

2. UNIX for Dummies Questions & Answers

Unable to understand RAID PARTITION

Hi i am studying about raid partion.i am not able to understand RAID level 5. below is excerpt taken from tutorial. RAID level 5 are they trying to say that the will be one extra disk which contain all the data. let says there are 4 disk. out of 4 , 3 disk are used for storing data and... (15 Replies)
Discussion started by: scriptor
15 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

5. Shell Programming and Scripting

Unable to understand if condition

Hi geeks, I am trying to understand below if statement. can someone please explain me meaning of if condition. if ] then echo -e "1" fi Thanks Please use CODE tags. (3 Replies)
Discussion started by: jagnikam
3 Replies

6. UNIX for Dummies Questions & Answers

Unable to understand ps output.

I m executing ps command and sorting it according to memory usage. Please find the output of the command. # ps aux --sort pmem USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 2060 624 ? Ss 01:54 0:00 init root 2 0.0... (1 Reply)
Discussion started by: pinga123
1 Replies

7. Shell Programming and Scripting

Need help to understand Awk code.

Hi Guys, Can someone please explain this code to me. I could figure out it's adding and comparing two fields but I am not sure which ones. sort -t"|" -k3.1 /tmp/mpcashqc.xtr| awk -F"|" '{CHECKAMT+=$3;BATCHTOT=$4;\ items++}END{for(i in CHECKAMT) if (CHECKAMT!=BATCHTOT)... (6 Replies)
Discussion started by: nua7
6 Replies

8. Solaris

Unable to understand disk layout and where are the free space

Hi I am unable to understand the disk layout of one of my disk attached to v240. This is newly installed system from jumpstart. I am unable to see the free space on backup slice 2 and there are 0 to 8 slices listed when I run format and print the disk info, also there is no reference of... (9 Replies)
Discussion started by: kumarmani
9 Replies

9. UNIX for Dummies Questions & Answers

unable to understand a awk code

i am not able to understand the following code for awk: $awk -F"|" '{ kount++} >END { for (desig in kount) > print desig,kount }' emp.list the input file i.e. emp.list is :: 3432| p.k.agrwal |g.m |sales 4566|g.l.sharma |director|production 3433|r shah | g.m | production... (1 Reply)
Discussion started by: streetfi8er
1 Replies

10. UNIX for Dummies Questions & Answers

Unable to understand associative nature of awk arrays

About associative nature of awk arrays i'm still confused, not able to understand yet how array element can be accessed based on a string, I got one example at gawk manual to illustrate associative nature of awk arrays, it goes here: Codeawk ' # Print list of word frequencies { for (i = 1;... (3 Replies)
Discussion started by: nervous
3 Replies
Login or Register to Ask a Question