Issue while storing grep result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue while storing grep result
# 1  
Old 07-31-2008
Issue while storing grep result

Hi All,

Command: grep -i -n "rule" *.err *.log | grep -v "SP_RULE"
The above command when run gives me two -three lines of output.
Now I am storing the result of the grep command
ie,
ruleerrors=`grep -i -n "rule" *.err *.log | grep -v "SP_RULE"`
Now when I echo the value of ruleerrors, I am getting a large result similar to result of “ls” command along with the actual desired output.

What can be cause ?
Sreejith_VK
# 2  
Old 07-31-2008
Add double quotes around the variable name to avoid wildcard expansion.
# 3  
Old 07-31-2008
Thanks era.
It worked.
Sreejith_VK
# 4  
Old 07-31-2008
Issue while storing grep result

What is the difference between echo $ruleerrors and echo "$ruleerrors"?

Thank you
# 5  
Old 07-31-2008
That is pretty much the difference. See a shell quoting tutorial such as UNIX Shell Quote for a detailed explanation.
# 6  
Old 07-31-2008
Issue while storing grep result

Thanks Era (for the link).

One thing is not clear to me is ...

grep returned multiple rows and the output is redirected to a variable then
-will it be saved in a single line (with hidden characters)? or in multiple lines ?

Could you help me if I am missing some thing from your message?
you said "Add double quotes around the variable name to avoid wildcard expansion"


Thank you.
# 7  
Old 07-31-2008
The value is saved with newlines and everything. echoing it back without quoting will flatten any whitespace to single spaces. (This is a feature of the shell and its quoting mechanisms, not of echo.) To make sure you see the real actual value, examine the variable with set.

Code:
sh$ var=`perl -e 'print "Output\nwith\ \ \ spaces\t\ \nand newlines\n"'`
sh$ echo $var
Output with spaces and newlines
sh$ echo "$var"
Output
with   spaces	 
and newlines
sh$ set | grep var=
var=$'Output\nwith   spaces\t \nand newlines'

This is with bash; output will probably be slightly different with other shells.

Notice that the final trailing newline is chomped off by the shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with the shell script after storing the directory in a variable

FTP is connecting to the server but i am getting an error - Enter if the env is dev or test or prod: test Please enter the id no : xxxxxxx Connected to xxxx 220 (vsFTPd 2.2.2) 331 Please specify the password. 230 Login successful. ?Invalid command ?Invalid command ?Invalid command... (3 Replies)
Discussion started by: chandraprakash
3 Replies

2. Shell Programming and Scripting

Grep result from dd command

Hi, I am running following command in a bash script for testing IO and use grep to get throughput number, but it did not work, it displayed everything: dd if=/dev/zero of=/dev/null bs=1G count=1 oflag=dsync | grep bytes | awk '{print $7}' 1+0 records in 1+0 records out 536870912 bytes... (2 Replies)
Discussion started by: hce
2 Replies

3. UNIX for Dummies Questions & Answers

Sftp using batchfile - storing result local

I'm making an sftp-connection to a remote server. I want the result of an ls-command in a local file and the result of ls on another folder in another local file. Because everything has to go as fast a possible I wan't to do everyting in one connection. The command I use is : psftp -v -batch -b... (4 Replies)
Discussion started by: pistach
4 Replies

4. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

5. Shell Programming and Scripting

Storing Grep Files into Var and comparing it

Hi, I was working on the password policy settings of Solaris where i wanted to grep the results of MINDIFF and comparing it to if else to make it into a auditing script. I stored the grep into VAR1 and compare it if MINDIFF=3 but it doesnt work. Can anyone help me with it? !#/bin/bash ... (2 Replies)
Discussion started by: Jareddd
2 Replies

6. Shell Programming and Scripting

Pattern matching & storing result in variable

Hi! i'm trying to parse textfiles against a pattern and storing the result in a variable. The strings i want to get are embraced by and can occur several times in one line, so e.g. some text anything else endwhat i have so far: #!/bin/bash for f in $* do exec 3<&0 exec 0<$f ... (2 Replies)
Discussion started by: thoni
2 Replies

7. Shell Programming and Scripting

pipe result from grep

Trying to create a command line script to look for all files matching a pattern, grep for a specific value in each file, and write out the filename long list. It's possible the filename won't containe the value. { echo “Running....” for fname in 811_Intermediate_File_* do grep -l... (3 Replies)
Discussion started by: gavineq
3 Replies

8. Shell Programming and Scripting

storing result of a command in variable

For whatever reason I cant seem to fix my syntax to do the following. I want to run a grep and count how many instances come up and store that number in a variable but I keep erroring out. Here's my code in bash: number=grep blah file.txt | wc -l (1 Reply)
Discussion started by: eltinator
1 Replies

9. Shell Programming and Scripting

Problem storing SSH result in a variable

i have this SSH command which runs perfectly on command prompt in sunOS ssh -o Port=${portno} ${uname}@${server} find ${dir_path} -name '***' output : /usr/local/home/*** My problem is when i run same command in my script #!/usr/bin/ksh res=`ssh -o Port=${portno} ${uname}@${server}... (1 Reply)
Discussion started by: prash184u
1 Replies

10. UNIX for Dummies Questions & Answers

To have a numeric result from grep

I am new to unix. i need to know how to use grep to grep and expression from a file. and pass the result as a 0 for found and 1 for not found. I can only go up to grep 'Checking Subscription Status' ranos.log. Please help. Thank you. (2 Replies)
Discussion started by: Hak Dee
2 Replies
Login or Register to Ask a Question