Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for spaces in fields with Regular Expression

Status
Not open for further replies.

Jarrod13

Programmer
Dec 18, 2008
12
Hi,

I need help writing a regular expression to check for anything >= to 1 space.

That is, if my record seperator is a "|" if I have a field that is | | or | | or | | I want the regular expression to be able to check for that.

Thanks for the help

 

I'm not good at regular expressions, but this would do it:
Code:
awk -F'|' '
{f=""; 
 for (i=1;i<=NF;++i)
 {if (length($i)==0) f=f" "i;
  else if (index($i," ")>0) f=f" "i;
 }
 if (length(f)>0) print "Line "NR" has fields ("f" ) w/blanks.";
}' My_File.csv
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thank you,

I also came up with this:

Code:
x ~ "^[ \t]*$"

 

Nice, unfortunately it wont work with some "embeded" blanks:
Code:
$ cat myScript
#!/bin/ksh
cat - <<! >tmp
hello|we care| more
002|8|333|9
elka|brown| |thebest
auto||vwbetter|todrive
!
awk -F'|' '
{f=""; for (i=1;i<=NF;++i){ if ([red]$i ~ "^[ \t]*$"[/red]) f=f" "i;}
 if (length(f)>0) print "Line "NR" has fields ("f" ) w/blanks.";
}' tmp
exit

$ myScript
Line 3 has fields ( 3 ) w/blanks.
Line 4 has fields ( 2 ) w/blanks.
$

Line 1 is missing!
[ponder]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top