Grep with variable not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep with variable not working
# 1  
Old 02-11-2016
Grep with variable not working

Hello,

I am using below code :

Code:
export ORAR=orp712z.int.thomsonreuters.com
#echo $ORAR;
if echo $ORAR|grep -i "_"
then
ORACLE_SID1= echo $ORAR|cut -f2 -d "_"
echo $ORACLE_SID1
ORACLE_SID=fgrep "$ORACLE_SID1" /etc/oratab|cut -f1 -d ":"
#echo $ORACLE_SID
else
ORACLE_SID1= echo $ORAR|cut -f1 -d "."
echo $ORACLE_SID1
ORACLE_SID=grep "$ORACLE_SID1" /etc/oratab|cut -f1 -d ":"
echo $ORACLE_SID
fi


Code:
c643upetcmdr {/home/oracle}: cat /etc/oratab
orp709z1:/u01/app/oracle/product/11.2.0.4/db:N
orp710z1:/u01/app/oracle/product/11.2.0.4/db:N
orp712z1:/u01/app/oracle/product/11.2.0.4/db:N
orp713z1:/u01/app/oracle/product/11.2.0.4/db:N


Now, the script should print orp712z1 but this is printing orp709z1.


Best regards,
Vishal
# 2  
Old 02-11-2016
Try:
Code:
ORACLE_SID1=$(echo $ORAR|cut -f1 -d ".")


Or better, using parameter expansion:
Code:
ORACLE_SID1=${ORAR%%.*}


likewise:
Code:
ORACLE_SID1=$(echo $ORAR|cut -f2 -d "_"[COLOR="red"])

or
Code:
tmpSID=${ORAR%"_${ORAR#*_*_}"}
ORACLE_SID1=${${tmpSID#*_}

or:
--
alternative

Code:
IFS=_ read x ORACLE_SID1 x << EOF
$ORAR
EOF


Last edited by Scrutinizer; 02-11-2016 at 01:27 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command is not working

I have made a program that reads a text file and checks for palindromic words and then outputs them. They each appear on a new line with a count of the number of occurences beside each of the words. Requirements for being classed as palindrome are that the word must have at least 3 letters and... (7 Replies)
Discussion started by: greenhouse91
7 Replies

2. Shell Programming and Scripting

Grep not working on mac

Hi all, I got a new mac and can't get grep, awk etc to work. I tried the following command: grep DICER test.txt output: AGOER text.txt looks like this: DICER DICER AGOWhat is wrong? Please use code tags (23 Replies)
Discussion started by: Palgrave
23 Replies

3. Shell Programming and Scripting

Variable not working in grep from script

Hi, If I hard code a value in the grep it works fine from script, when I use variable it doesn't work. On a seperate note, some lines (during testing) works fine from command line but not from scirpt. #!/bin/bash # Will fetch the (oldest - as ls will sort by name by default)Date in the... (7 Replies)
Discussion started by: krish.m
7 Replies

4. Shell Programming and Scripting

Working with grep and Bash

Hi, I am currently working on a Bash shell script that - Downloads a webpage, in this case youtube.com - Extracts Number of views, Extracts Title of video, Extracts User who made it, and lastly Duration. Then I have to Out put this into columns. To me this sounds like crazyness. I'm very new... (6 Replies)
Discussion started by: Njzangel
6 Replies

5. UNIX for Dummies Questions & Answers

grep'ing a variable - why not working

Hi all, Am writing a ksh script where I am looking for processes that has gone defunct and all of which has the same PPID PID is the variable that I need to match as this is the process ID of the processes that has gone defunct Am just curious how come the following DOES NOT work? ps... (6 Replies)
Discussion started by: newbie_01
6 Replies

6. UNIX for Dummies Questions & Answers

grep -f not working

Hello, I'm going crazy about this. I'm using grep to filter some values as in pas -ef | grep asterisk. When I use the same with -f somefile something weird happens, if somefile is created with vi it'll work, if somefile is created with vi but values are pasted from an Excell file it will not work.... (2 Replies)
Discussion started by: seveman
2 Replies

7. Shell Programming and Scripting

grep not working ????

Hi, I've prob in doing grep. I want to grep line staring with number 531250 in the 1st column from a file (example in picture attached below) using command grep -w "531250" file my ideal result should be 531250 1 21 42.1 100 1e-05 ... (8 Replies)
Discussion started by: masterpiece
8 Replies

8. UNIX for Dummies Questions & Answers

grep not working

This condition is not able to grep , can any one tell what's wrong with this part. I am able to see from unix command but not with host script. echo "Checking for Loader Status " >> $REPFILE if test $? = 0 then echo "Successful termination of SQL*Loader "$LOADER1 >>... (5 Replies)
Discussion started by: u263066
5 Replies

9. UNIX for Advanced & Expert Users

cat and grep not working

I am trying to cat a file and then grep that file for a number. I can do it fine on other files but this particular file will not do anything. I tried running it on an older file from the same device but it is just not working. The file is nothing more than a flat file on a unix box. Here is just a... (3 Replies)
Discussion started by: jphess
3 Replies

10. Solaris

grep -r isn't working

Hi, I was trying to use this particular option of grep grep -r 'Search_pattern' * This command should ideally search all the occurrences of Search_pattern recursively within a directory & print it on shell prompt. But this command is not doing what is expected. It just displays nothin! ... (8 Replies)
Discussion started by: harishmitty
8 Replies
Login or Register to Ask a Question