Using case instead of grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using case instead of grep
# 1  
Old 09-24-2016
Using case instead of grep

im using a case statement to search for a pattern in a variable, like this:

Code:
case "${VARIABLE}" in
* unix is great *)
    echo Found it!
;;
esac

However, what happens if I want to make sure some other string DOES NOT exist on the same line that "unix is great" is on??

with egrep, i can solve it with something like:

Code:
echo "${VARIABLE}" | egrep -v "bad lines i dont want" | egrep "unix is great"

I'm hoping there's a way to do this in case, without having to use any external tools.
# 2  
Old 09-25-2016
Try:
Code:
case $VARIABLE in 
  *"bad lines i dont want"*)
    :                            
  ;;
  *"unix is great"*)
    echo "Found: $VARIABLE"
  ;;
esac


Last edited by Scrutinizer; 09-25-2016 at 02:47 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 09-25-2016
Quote:
Originally Posted by Scrutinizer
Try:
Code:
case $VARIABLE in 
  *"bad lines i dont want"*)
    :                            
  ;;
  *"unix is great"*)
    echo "Found: $VARIABLE"
  ;;
esac

I tried this but it didn't seem to work. when i run it, i get nothing back. just the prompt.

Code:
#!/bin/sh

VARIABLE='bad lines i dont want
unix is great'
case ${VARIABLE} in 
  *"bad lines i dont want"*)
    :                            
  ;;
  *"unix is great"*)
    echo "Found: $VARIABLE"
  ;;
esac

And I tried putting the variable in quotes:

Code:
#!/bin/sh

VARIABLE='bad lines i dont want
unix is great'
case "${VARIABLE}" in 
  *"bad lines i dont want"*)
    :                            
  ;;
  *"unix is great"*)
    echo "Found: $VARIABLE"
  ;;
esac

neither worked
# 4  
Old 09-25-2016
Of course not. There is only one variable and the expansion of that variable contains the string bad lines i don't want which is matched by the 1st pattern in your case statement (which produces no output).

If you're dealing with a variable containing multiple lines and you want to process each line separately, you need something more like:
Code:
#!/bin/sh
VARIABLE='this line should not print -- bad lines i dont want
this line should print -- unix is great
neither of the above'

printf "%s\n" "$VARIABLE" | while IFS= read -r line
do	case "$line" in
	*"bad lines i dont want"*)	echo "1st case: $line";;
	*"unix is great"*)		echo "2nd case: Found: $line";;
	*)				echo "default case: No match: $line";;
	esac
done

which produces the output:
Code:
1st case: this line should not print -- bad lines i dont want
2nd case: Found: this line should print -- unix is great
default case: No match: neither of the above

These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-27-2016
The behaviour of the case statement matches what your need actually.
see:
Code:
$ cat case.sh
for VARIABLE in 'bad lines i dont want unix is great' 'bad line ... unix is great'
do
((LOOP+=1))
case ${VARIABLE} in
  *"bad lines i dont want"*)
    echo "Loop : $LOOP :CASE 1: $VARIABLE"
  ;;
  *"unix is great"*)
    echo "Loop : $LOOP :CASE 2: $VARIABLE"
  ;;
esac
done

$ ./case.sh
Loop : 1 :CASE 1: bad lines i dont want unix is great
Loop : 2 :CASE 2: bad line ... unix is great

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ksh: Send a mail in case grep finds something

I want to search a file if it contains special strings and if yes, the records found should be mailed. I can either do it with a temporary file: /usr/bin/grep somestring somefile > /tmp/tempfile && /usr/bin/mail -s "Found something" email@mycomp.com < /tmp/tempfile... or by running the grep... (10 Replies)
Discussion started by: Cochise
10 Replies

2. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

3. UNIX for Advanced & Expert Users

penalty for case insensitive grep

I just found out there were a big performance penalty for case insensitive "grep" on big files. It would be understandable, except that the penalty seems to be exaggerated out of proportion. A real example, if I only grep a single letter "V" (or "v") , without "-i", on a big file, (file... (10 Replies)
Discussion started by: phil518
10 Replies

4. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

5. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

6. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 Replies

7. Shell Programming and Scripting

grep with case or if else help

I am trying to do a directory search using grep to find all words from a txt file. And I am not getting correct results ..can someone help me out ...below is my code.. #1/bin/sh for word in `cat test.txt` do grep -ir "$word" /cygdrive/c/test /cygdrive/d/test1 case $? in 0) echo "$word" >>... (3 Replies)
Discussion started by: kchandooo
3 Replies

8. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

9. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question