Awk to find space and tab. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Awk to find space and tab.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-01-2009
pinnacle pinnacle is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 8 May 2014, 12:25 PM EDT
Posts: 242
Thanks: 9
Thanked 1 Time in 1 Post
Awk to find space and tab.

Wants to print line when there exist leading or trailing space or tab in fields 2,3 and 5

The below code prints all lines in file even if they dont have leading and trailing space or tab.

Code:
nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]*" || $i ~ "[ \t]*$")}}1' file

file
Quote:
newyork|times square| centralpark|abc|def |t
washington| capitol | museum |abc|def |t
texas|dallas|san antonio|abc|def|t
Ouput required:
Quote:
newyork|times square| centralpark|abc|def |t
washington| capitol | museum |abc|def |t
Thanks
Sponsored Links
    #2  
Old 06-01-2009
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 11 Times in 11 Posts
Change the * to +. You have made the extremely common mistake of using an REGEX with a zero length match.
Sponsored Links
    #3  
Old 06-01-2009
pinnacle pinnacle is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 8 May 2014, 12:25 PM EDT
Posts: 242
Thanks: 9
Thanked 1 Time in 1 Post
Quote:
Originally Posted by reborg View Post
Change the * to +. You have made the extremely common mistake of using an REGEX with a zero length match.

Code:
 nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]+" || $i ~ "[ \t]+$")}}1' file

still the same thing.
And i want to look 2,3 and 5th field only not all the fields
    #4  
Old 06-01-2009
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 11 Times in 11 Posts
Yes, but you also have a '1' there, so you are printing every line. You need to delete that too, any you do nothing in the for loop/if statement.

Just check the field number in the for lop and print out the lines you want.
Sponsored Links
    #5  
Old 06-02-2009
pinnacle pinnacle is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 8 May 2014, 12:25 PM EDT
Posts: 242
Thanks: 9
Thanked 1 Time in 1 Post
Quote:
Originally Posted by reborg View Post
Yes, but you also have a '1' there, so you are printing every line. You need to delete that too, any you do nothing in the for loop/if statement.

Just check the field number in the for lop and print out the lines you want.
I removed 1 and now i want to check for leading and trailing space or tab on each field.
The below code returns nothing.

Code:
nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]+" || $i ~ "[ \t]+$")}}' file

Help appreciated
Sponsored Links
    #6  
Old 06-03-2009
pinnacle pinnacle is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 8 May 2014, 12:25 PM EDT
Posts: 242
Thanks: 9
Thanked 1 Time in 1 Post
Quote:
Originally Posted by pinnacle View Post
I removed 1 and now i want to check for leading and trailing space or tab on each field.
The below code returns nothing.

Code:
nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]+" || $i ~ "[ \t]+$")}}' file

Help appreciated
I got the following code to work
But would like to remove sort -u and handle it with in awk

Code:
nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]+" || $i ~ "[ \t]+$") print $0}}' file | 
sort -u  > outfile

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
for i in `find *` breakdown since the directory name has space patiobarbecue Shell Programming and Scripting 3 12-09-2008 10:44 AM
How to find the free space & usage of the particular directory in Hp-Unix? bobprabhu UNIX for Dummies Questions & Answers 2 06-27-2008 01:05 PM
Find lines with space between strings Galt Shell Programming and Scripting 5 05-07-2008 02:06 PM
How to find the top 6 users (which consume most space)? RebelDac AIX 5 09-17-2007 06:36 PM
How to find the size of Process Address space. S.Vishwanath UNIX for Dummies Questions & Answers 1 07-10-2001 08:46 AM



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