Grep from a file variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep from a file variable
# 1  
Old 10-30-2003
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 [ ${filecontents} = "Object does not exist." ]; then
delete file
But I get an error, grep: can't open leave_crew_allow_v.txt. Where leave_crew_allow_v.txt is the actual name assigned to the variable.




Smilie
# 2  
Old 10-30-2003
i am unsure what you mean by "If the out put = Object does not exist. I need to delete the output file."

but if your going to test for stuff in ksh use the "file atribute operators" to do the testing for you.
# 3  
Old 10-30-2003
The text is redirected to a file, I need to verify that the script actually returned something useful, rather than "Object does not exist"

Hopefully my grep command looks at the output file contents, to verify that the script actually returned useful data, if not delete all related files.

I hope this is more insightful.

Thanks for the quick response.
# 4  
Old 10-30-2003
I think your script needs a couple tweaks. The initial pattern you're searching for with grep should be surrounded by quotes. And there's an easier way to see the result of the grep command by using $?.
Code:
grep -qi "Object does not exist." $1.txt

if [ $? == 0 ]; then
 rm $1.txt
fi

Quote:
From the man page for grep:
-q
Quiet. Nothing is written to the standard output. grep exits with status 0 if an input line is selected. This provides a quick and easy method of testing if a pattern or string exists in a group of files.
# 5  
Old 10-30-2003
I am using a Korn shell.

I get the response form the script of,

Object does not exist.
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .

When I try without the q, i get can open file again.

Sorry to be a pain.

Smilie
# 6  
Old 10-30-2003
I have made a major blunder, I forgot to put the paht to the output file, Doop...

I have it working perfectly now.

Thanks for the bearing with my ravings and bumblings.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep variable

I've got a file that I'm trying to grep through that looks like this: alpha1 alpha2 alpha3 beta1 beta2 gamma5 gamma6 gamma7 gamma8 gamma9 and I want the output to only contain the line with the highest value for each, so the output I want is: alpha3 beta2 gamma9 I also need... (11 Replies)
Discussion started by: tiberione
11 Replies

2. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: dbashyam
2 Replies

3. 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

4. Shell Programming and Scripting

grep using variable

I have a pattern like: column "5" is missing PS: the no is in double quotes. The number usally changes, so we use a loop to grep. grep 'column "$number" is missing' filename.txt But it is not working.... How to solve this? (2 Replies)
Discussion started by: karumudi7
2 Replies

5. Shell Programming and Scripting

Grep through a variable

I want to search a text in file but that file is pointing to variable. ex: file=there was nothing special grep "there was nothing" $file but its not working . Can u let me know that how we can use variable($file) in grep command. Please use code tags (6 Replies)
Discussion started by: allthanksquery
6 Replies

6. Shell Programming and Scripting

grep using variable

how can I use grep with a variable to find a value? cat data.out Hello World grep "Hello World" data.out Hello World ## Value found I want to do something like this but can't seem to get it to work any suggestions would be appreciated. var="Hello World" grep $var data.out (3 Replies)
Discussion started by: BeefStu
3 Replies

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

How to grep a variable?

Hi, I'd like to grep a variable that I saved in the program. Like grep '0\$variable1' file1 Does someone know what's wrong with this command? Thanks a lot! (2 Replies)
Discussion started by: whatisthis
2 Replies
Login or Register to Ask a Question