command to grep the variable from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting command to grep the variable from file
# 1  
Old 11-23-2011
command to grep the variable from file

Hi,

I am looking for the following:

1. A command to 'grep' the variable from a file
2. And check whether the entered variable is empty in that file

I have a file as given below:

IAGLOBAL_USERID=admin

I want to check with a whether the variable contains a value, say here its admin if it contains value I will proceed ahead else I should give three tries for the user if in those three tries if the user does not enter the userid then I will exit.

I did the following:

Code:
var1=`cat $NCO/response_uid.txt | grep -i IAGLOBAL_WASUserID | cut -d'=' -f1`
var2=$(echo "${var1}" | sed -e 's/^ *//g;s/ *$//g')
echo $var2
if [ -z "$var2" ];
then
rm -rf $NCO/response_uid.txt
rm -rf $NCO/response_def.txt
exit 1;
fi

Any ideas?

Thanks,
Dinesh
# 2  
Old 11-23-2011
Why IAGLOBAL_WASUserID?
# 3  
Old 11-23-2011
To grep the variable, you can do :
Code:
var=`awk -F'=' '/IAGLOBAL_USERID/{sub(/[ \t]+$/, ""); print $2}' $NCO/response_uid.txt`


Last edited by thanhdat; 11-23-2011 at 08:50 AM.. Reason: space
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue using empty variable in grep command

Hi, I'm writing a shell script and trying to grep a variable value, it works fine as long as there is a value in /tmp/list.out which is captured in $DSK but sometimes the file tends to be empty and that is where I'm having an issue while using grep which returns nothing. I know I can use something... (15 Replies)
Discussion started by: mbak
15 Replies

2. Shell Programming and Scripting

Grep command to find string in Variable in file2

Hi , I am executing 2 queries and output is saved in file1.txt and file2.txt example of file1.txt Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,, Testing Excel _.xlsx,/Lab/Development and... (3 Replies)
Discussion started by: Sunil Mathapati
3 Replies

3. Shell Programming and Scripting

Directing cat or grep command in variable

Hi, I still have the problem with directing information from cat or grep to a variable. For instance: XMSG "$(date +%Y_%m_%d)_error_report.txt" "$(cat "$(date +%Y_%m_%d)_error_report.txt")" &Works! The text received by cat is directed to my function. If it is written like this, my... (2 Replies)
Discussion started by: haukee
2 Replies

4. Shell Programming and Scripting

(BASH) Using a loop variable to grep something in a file?

Hi, I have a loop running until a variable L that is read previously in the full script. I'd like to grep some information in an input file at a line that contains the value of the loop parameter $i. I've tried to use grep, but the problem is nothing is written in the FILE files. It seems grep... (5 Replies)
Discussion started by: DMini
5 Replies

5. Shell Programming and Scripting

Grep Variable From File

Hey guys, I'm attempting to compare the contents of two files that are not identical on a Solaris 10 box. We are under audit and I need to do some comparisons of our application accounts with our unix accounts. I essentially want to pull a line from one file and search the second for an equal... (1 Reply)
Discussion started by: kaledragule
1 Replies

6. Shell Programming and Scripting

Grep to return a code from accessing variable file name

All I want to do is find out if the string 'NO' is in a file by checking for a return code of 0 if there is a match. The grep works fine on the command line directly naming the file but I am obviously not using the correct syntax within the shell script as I consistently get the error message ... (5 Replies)
Discussion started by: SusanDAC
5 Replies

7. Shell Programming and Scripting

read in variable data from another file - grep

Hello! I think this should be an easy solution. I have a large file with many fields of data. The first field has a unique identifier (a subject number) for every record for a chunk of data. Something like this: There were ten experimental conditions (ec), but the ec is identified by only... (11 Replies)
Discussion started by: ccox85
11 Replies

8. Shell Programming and Scripting

how to store grep command in a Variable

I have a variable A echo $A 5060 I am exporting the value X,Y,Z and it id fetching right thing and When I run C=`${X} -l ${Y} ${Z} "trap '' INT;. ~/.profile >/dev/null 2>/dev/null; netstat -na | grep \$A`" here it is going to same directory and also running netstat -na | grep 5060 ... (4 Replies)
Discussion started by: madhusmita
4 Replies

9. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

10. UNIX for Dummies Questions & Answers

Grep from a file variable

I am wanting to test the output from a script. If the out put = Object does not exist. I need to delete the output file. I am using , filecontents=`grep -i Object does not exist. $1.txt` then I test the variable with, if ; then delete file But I get an error, grep: can't open... (5 Replies)
Discussion started by: jagannatha
5 Replies
Login or Register to Ask a Question