|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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 514In 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
|
||||
|
||||
|
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
|
||||
|
||||
|
toupper, tolower converts the argument to upper or lowercase...
So what was returned is what is expected... |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Quote:
Code:
awk '{if (toupper($1) ~ /A/) print $0}' invCode:
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
|
||||
|
||||
|
Then don't use toupper: Code:
awk '$1~/^A/' inv |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|