Egrep ERE to find null


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep ERE to find null
# 1  
Old 05-02-2013
Egrep ERE to find null

Hello All,
My apologies if a similar question has already been posted. I searched for "grep", "egrep" and "null"

I'm having an issue with egrep and an extended regular expression.
I'm doing a test for a job name. I would like to have the user enter a job name with no spaces and also test to make sure that they don't accidently hit the return key too fast.
My code:
Code:
echo "Please enter a job name (no spaces): "
read jobname
#  ***test not working*** while [`echo $jobname | egrep '(" "|"^$")' | wc -l -gt 0 ]; do
 
while [`echo $jobname | grep " " | wc -l -gt 0 ] || [`echo $jobname | grep "^$" | wc -l -gt 0 ]; do   # working
echo "Please enter a new job name (NO spaces): "
read jobname
done

The command egrep '(" "|"^$")' isn't working!!! It doesn't give any errors but it never goes through the while statement from any test.
Thanks in advanced!!
# 2  
Old 05-02-2013
It is safer to have while not followed by the "okay" expression.
The following ensures there is only and at least one non-blank characters:
Code:
while ! echo "x$jobname" | egrep '^x[^[:blank:]]+$' >/dev/null
do
 echo "Please enter a new job name (NO spaces): "
 read jobname
done

The quotes are necessary for safety. Also further references should quote "$jobname".
The x is for additional safety, e.g. to escape a user input "-n" that has a special meaning in echo.
More strict is to test for at least one alphanumeric character:
Code:
while ! echo "x$jobname" | egrep '^x[[:alnum:]]+$' >/dev/null


Last edited by MadeInGermany; 05-02-2013 at 01:21 PM..
# 3  
Old 05-02-2013
Code:
#! /bin/bash

echo "Enter job name:"
read x

while [ 1 ]
do
    echo $x | egrep -q "^$|^ *$"
    if [ $? -eq 0 ]
    then
        echo "No blank lines or spaces. Enter job name:"
        read x
    else
        break
    fi
done

echo $x

# 4  
Old 05-02-2013
There's not just one but many easy ways to test with pure shell builtins:

Code:
if [ -z "$VARTOTEST" ] || [ "$VARTOTEST" = " " ]
then
        echo "$VARTOTEST empty or one space"
fi

Code:
case "$VARTOTEST" in
) echo "blank" ;;
 ) echo "one space" ;;
*) echo "not blank" ;;
esac

Code:
VAR=""
case X in $VARTOTEST ; do VAR="$X" ; done
if [ -z "$VAR" ]
then
        echo "$VARTOTEST is blank, or one or more spaces"
fi

Code:
set -- $VARTOTEST

if [ -z "$1" ]
then
        echo "$VARTOTEST blank, or one or more spaces"
fi

And these are just the pure bourne ways. If I knew you had bash or ksh I could suggest simpler ways using their string operations.
# 5  
Old 05-02-2013
Code:
while [`echo $jobname | grep " " | wc -l -gt 0 ] || [`echo $jobname | grep "^$" | wc -l -gt 0 ]; do  # working

Are you sure that is "working"? The backticks starts before echo, but they don't stop. Compare:
Code:
while [`echo $jobname | grep " " | wc -l` -gt 0 ] || [`echo $jobname | grep "^$" | wc -l` -gt 0 ]; do  # working

# 6  
Old 05-02-2013
Thanks!!

This is what I ended up using that works
Code:
while [`echo $jobname | egrep '^[^[:blank:]]+$' | wc -l -gt 0 ]; do

MadeInGermany: I'm unaware of what the "x" in your example does. I'm not asking for a long winded explanation, although you may. If you don't mind give me a viable resource where I may read up on that functionality!

balaisuri: thanks for helping. I did try the ERE of "^$|^ *$" and it didn't work at least not on my system. it catches null but doesn't catch a line with a space.

Corona688: Thanks for the many responses. I'll have to wait till I have more time to go through your methods.
# 7  
Old 05-02-2013
Quote:
This is what I ended up using that works
That's way too convoluted. It may "work", but it's too confusing to put into long-term source code. And it is grammatically incorrect, as I pointed out before. I would look at the other suggestions that were made, as you indicated you want to do.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Egrep find word that occurs twice in a row

Hi there I am trying to figure out and understand what the syntax would be to egrep lines that have a word occur twice in a row. the two words obviously should have a space between them and also it has to be case sensitive which I believe grep is by deffault. the closest I have come is... grep... (7 Replies)
Discussion started by: spo_2138
7 Replies

2. Shell Programming and Scripting

To find record having null value

Hi All My requirement is to find the null values in particular column of a file and reject it in case if it contains null values. But the challenge is that I want a common command which can be used across different file, as the position of the column we need to check for different file may get... (14 Replies)
Discussion started by: ginrkf
14 Replies

3. Shell Programming and Scripting

How to find null column?

hi Gurus, I need find the null column in a file. my file like below abc, ,cde,def abc,ded,cdd,def abc, ,ddd,ccd aaa,bbb,ccc,ddd basic, I need to find the lines which second column is null Thanks in advance (3 Replies)
Discussion started by: ken6503
3 Replies

4. Shell Programming and Scripting

egrep help required to find pattern

Hi All, Can some one please help me how to grep the comments from "oracle" & "sybase" code. I would like to grep below type of pattern. -- /* */ Please help. (6 Replies)
Discussion started by: gr8_usk
6 Replies

5. Shell Programming and Scripting

how to find null column

Hi, everyone I have a requirement as following: source file 1, abc, def, caaa 2, , cde, aaa 3, bcd, , adefefg I need find columns which contains null value, in above example, I need get two rows 2, , cde, aaa 3, bcd, , adefefg anybody has idea how to achive this ... (5 Replies)
Discussion started by: ken002
5 Replies

6. Shell Programming and Scripting

Find out if few fields in a file are null

Hi, I've a pipe delimited file where I want to find out a number of lines where 1st 2nd and last field are null using awk/sed. Is it possible? Thanks (5 Replies)
Discussion started by: rudoraj
5 Replies

7. UNIX for Dummies Questions & Answers

Using /dev/null with grep and find

Hi, I am trying to display the filename in which a string was found after using find and grep. For this after some googling I found that this works: find -name "*.java" -exec grep "searchStr" {} /dev/null \; I wanted to know the difference between the above and the following: find -name... (0 Replies)
Discussion started by: gaurav_s
0 Replies

8. Shell Programming and Scripting

How can find Null value in If condition

Hi, i wrote If Conditions in my script, it's returns null and some values. but i am unable to find when Null value getting. bec we need modification according null vales. pls help me on this. (2 Replies)
Discussion started by: koti_rama
2 Replies

9. Shell Programming and Scripting

Find null fields in file

Hi All, I have some csv files out of which i want to find records which have empty values in either the 14th or 16th fields. The following is a sample. $cut -d',' -f14,16 SPS* | head -5 VOIP_ORIG_INFO,VOIP_DEST_INFO sip:445600709315@sip.com,sip:999@sip.com... (2 Replies)
Discussion started by: rahulrathod
2 Replies

10. Solaris

How to find egrep version on Solaris 10

Hi, How can I find the egrep version installed on Solaris 10 as I don't see any egrep --version option.Also wanted to know which version would support option of -A (eg. egrep -A NUM PATTERN FILE ) Print NUM lines of trailing context after matching lines. Thanks & Regards, Kiran. (6 Replies)
Discussion started by: kiranherekar
6 Replies
Login or Register to Ask a Question