Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-08-2012
Registered User
 
Join Date: Jul 2005
Posts: 94
Thanks: 11
Thanked 0 Times in 0 Posts
Awk Help - toupper/tolower

Hi,

I am learning awk and faced few queries. Kindly suggest on the same. Where it is wrong.


Code:
$ awk '{if (toupper($1) ~ /a/) print $0}' inv 

$ awk '{if (toupper($1) ~ /A/) print $0}' inv -- Why this output
Jan  13  25  15 115
Mar  15  24  34 228
Apr  31  52  63 420
May  16  34  29 208
Aug  15  34  47 316
Jan  21  36  64 620
Mar  24  75  70 495
Apr  21  70  74 514

It should be like this.
Apr  31  52  63 420
Aug  15  34  47 316
Apr  21  70  74 514

$ awk '{if (tolower($1) ~ /A/) print $0}' inv

$ awk '{if (tolower($1) ~ /a/) print $0}' inv  ---why this output
Jan  13  25  15 115
Mar  15  24  34 228
Apr  31  52  63 420
May  16  34  29 208
Aug  15  34  47 316
Jan  21  36  64 620
Mar  24  75  70 495
Apr  21  70  74 514

It should be like this.
Jan  13  25  15 115
Mar  15  24  34 228
May  16  34  29 208
Jan  21  36  64 620
Mar  24  75  70 495

Sponsored Links
    #2  
Old 06-08-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,561
Thanks: 14
Thanked 438 Times in 423 Posts
toupper($1) ~ /A/ => If the first field (converted to uppercase) contains 'A' ""anywhere"" in the string.

Try this instead: toupper($1) ~ /^A/ to filter out the first field ""starting"" with 'A'

And, I'm not able to understand the second requirement. Do you want to search in first field if second character is 'a'?

Last edited by balajesuri; 06-08-2012 at 07:57 AM..
Sponsored Links
    #3  
Old 06-08-2012
Registered User
 
Join Date: Jul 2005
Posts: 94
Thanks: 11
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by balajesuri View Post
toupper($1) ~ /A/ => If the first field (converted to uppercase) contains 'A' ""anywhere"" in the string.

Try this instead: toupper($1) ~ /^A/ to filter out the first field ""starting"" with 'A'

And, I'm not able to understand the second requirement. Do you want to search in first field if the second character is 'a'?
My concern is that in first case, it should pick "A" only not "a" irrespective of the position.


Code:
$ awk '{if (toupper($1) ~ /A/) print $0}' inv -- Why this output
Jan  13  25  15 115
Mar  15  24  34 228
Apr  31  52  63 420
May  16  34  29 208
Aug  15  34  47 316
Jan  21  36  64 620
Mar  24  75  70 495
Apr  21  70  74 514

It should be like this.
Apr  31  52  63 420
Aug  15  34  47 316
Apr  21  70  74 514

In second case, it should not pic "A" but "a"


Code:
$ awk '{if (tolower($1) ~ /a/) print $0}' inv  ---why this output
Jan  13  25  15 115
Mar  15  24  34 228
Apr  31  52  63 420
May  16  34  29 208
Aug  15  34  47 316
Jan  21  36  64 620
Mar  24  75  70 495
Apr  21  70  74 514

It should be like this.
Jan  13  25  15 115
Mar  15  24  34 228
May  16  34  29 208
Jan  21  36  64 620
Mar  24  75  70 495

    #4  
Old 06-08-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,561
Thanks: 14
Thanked 438 Times in 423 Posts
For first case, did you try this? toupper($1) ~ /^A/

For second case,

Code:
# awk 'toupper($1) !~ /^A/ && tolower($1) ~ /a/ {print}' inv
Jan  13  25  15 115
Mar  15  24  34 228
May  16  34  29 208
Jan  21  36  64 620
Mar  24  75  70 495

Sponsored Links
    #5  
Old 06-08-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,633
Thanks: 118
Thanked 256 Times in 245 Posts
toupper, tolower converts the argument to upper or lowercase...
So what was returned is what is expected...
Sponsored Links
    #6  
Old 06-08-2012
Registered User
 
Join Date: Jul 2005
Posts: 94
Thanks: 11
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by vbe View Post
toupper, tolower converts the argument to upper or lowercase...
So what was returned is what is expected...
How it has returned what is expected. Using CYGWIN_NT-5.1


Code:
awk '{if (toupper($1) ~ /A/) print $0}' inv


Code:
Input file :

$ cat inv

Jan  13  25  15 115
Feb  15  32  24 226
Mar  15  24  34 228
Apr  31  52  63 420
May  16  34  29 208
Jun  31  42  75 492
Jul  24  34  67 436
Aug  15  34  47 316
Sep  13  55  37 277
Oct  29  54  68 525
Nov  20  87  82 577
Dec  17  35  61 401

Jan  21  36  64 620
Feb  26  58  80 652
Mar  24  75  70 495
Apr  21  70  74 514

Should return as under :


Code:
Apr  31  52  63 420
Aug  15  34  47 316
Apr  21  70  74 514

rest are unwanted like :

Code:
Jan  13  25  15 115
Mar  15  24  34 228
May  16  34  29 208
Jan  21  36  64 620
Mar  24  75  70 495


Last edited by Scrutinizer; 06-08-2012 at 08:48 AM.. Reason: additional code tags
Sponsored Links
    #7  
Old 06-08-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,341
Thanks: 144
Thanked 1,754 Times in 1,591 Posts
Then don't use toupper:

Code:
awk '$1~/^A/' inv

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
toupper or tolower case of first letter of the line depending on another file louisJ Shell Programming and Scripting 16 01-01-2012 03:43 PM
issue with toupper in nawk Samingla Shell Programming and Scripting 9 04-22-2011 01:51 PM
tolower (static pointer + malloc + realloc) limmer Programming 4 01-22-2010 01:49 PM
Handling multiple fields of a database file for toupper() function in awk Priyanka Bhati Shell Programming and Scripting 1 12-02-2009 07:43 AM
How to apply a "tolower" AWK to a few parts of a document marconet85 Shell Programming and Scripting 5 05-06-2009 03:10 AM



All times are GMT -4. The time now is 04:35 AM.