Unable to understand awk script.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unable to understand awk script.
# 8  
Old 04-04-2014
Because there is at least a single space before and after the second column in the input file. As I said, it is a rather strange example, since using a single space before and after, like you did, is actually more restrictive than using zero or more spaces, since zero spaces would then also match (for example foo$1234.00bar) , so it serves no purpose whatsoever it might as well be left out.. Further it seems puzzling why there should be a strict match for two (or more) decimals. Also the trailing semi-colon after the print statements is unnecessary and the result could also be accomplished in a rather simpler way.

---edit---

Ah, now I see what you mean:
Code:
/ *\$[1-9][0-9] \.[0-9][0-9] */ { print $1,$2,$3,"*"; }

Yes that is different entirely, since that means there should be a singles space before the dot, so that would not match anything,

whereas previously there was asterisk instead of the space:
Code:
/ *\$[1-9][0-9]*\.[0-9][0-9] */ { print $1,$2,$3,"*"; }

so that meant zero or more digits ([0-9])...

Last edited by Scrutinizer; 04-04-2014 at 08:44 AM..
# 9  
Old 04-06-2014
Hi Scrutinizer,

you said
Code:
* means a match for zero or more occurrences of the preceding single character or sub-expression.

then you quote

Code:
Yes that is different entirely, since that means there should be a singles space before the dot, so that would not match anything,
/ *\$[1-9][0-9]\.[0-9][0-9] */

so if i introduce space before decimal in my input file then o/p must be change.
below is my change input file. i have added space before decimal.
Code:
Fruit Price/lbs Quantity
Banana $0.89 100
Peach $0.79 65
Kiwi $1 .50 22
Pineapple $1 .29 35
Apple $0.99 78

so, now as per you the o/p of
Code:
/ *\$[1-9][0-9]\.[0-9][0-9] */

must match with the O/p of
Code:
/ *\$[1-9][0-9]*\.[0-9][0-9] */

.

where the input file for this is
Code:
*\$[1-9][0-9]*\.[0-9][0-9] */

mentioned as below
Code:
Fruit Price/lbs Quantity
Banana $0.89 100
Peach $0.79 65
Kiwi $1 .50 22
Pineapple $1 .29 35
Apple $0.99 78

however, o/p is not the same . can you pls explain this behavior.

alsocan you please explain be what is the significanse or working of each astic in below line. for me it is behaving differently.
Code:
/ *\$[1-9][0-9]*\.[0-9][0-9] */

# 10  
Old 04-06-2014
Quote:
Originally Posted by scriptor
Hi Scrutinizer,

you said
Code:
* means a match for zero or more occurrences of the preceding single character or sub-expression.

then you quote

Code:
Yes that is different entirely, since that means there should be a singles space before the dot, so that would not match anything,
/ *\$[1-9][0-9]\.[0-9][0-9] */

You are misquoting me. I did not write that regex

Quote:
so if i introduce space before decimal in my input file then o/p must be change.
below is my change input file. i have added space before decimal.
Code:
Fruit Price/lbs Quantity
Banana $0.89 100
Peach $0.79 65
Kiwi $1 .50 22
Pineapple $1 .29 35
Apple $0.99 78

so, now as per you the o/p of
Code:
/ *\$[1-9][0-9]\.[0-9][0-9] */

must match with the O/p of
Code:
/ *\$[1-9][0-9]*\.[0-9][0-9] */

.
Nope. Those are different regexes with different outcomes. The first matches 2 digits before the dot, the second matches one or more digits, in both cases the first digit should be between 1 and 9.
Quote:
where the input file for this is
Code:
*\$[1-9][0-9]*\.[0-9][0-9] */

mentioned as below
Code:
Fruit Price/lbs Quantity
Banana $0.89 100
Peach $0.79 65
Kiwi $1 .50 22
Pineapple $1 .29 35
Apple $0.99 78

however, o/p is not the same . can you pls explain this behavior.

alsocan you please explain be what is the significanse or working of each astic in below line. for me it is behaving differently.
Code:
/ *\$[1-9][0-9]*\.[0-9][0-9] */

* zero or more spaces
[0-9]* zero or more digits
* zero or more spaces
 
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