how to redirect the output of a grep command to a file inside a shell script

hi,

i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents inside.

can anyone help me out with this.

eg: grep -v 'rows' GREP_FILE > GREP_DEST

when i am trying this in the unix prompt i am getting the required output like the GREP_DEST file will have an output with all the lines having 'rows' removed.

when i am giving the same command inside my shell script, its not working..

thanks in advace for all your help..

Duplicate thread
http://www.unix.com/unix-dummies-questions-answers/69616-how-get-output-grep-command-file-inside-shell-script.html\#post302205946

hi,

i tried giving the full path as well... but still not working.. even i wanted to use 'awk' command like this... redirecting the output to some other file.. that also wasnt working... but working outside in the unix prompt..

please help me.. its urgent.. :slight_smile:

thanks,
kripa.

try like this
create the file which is used to load

touch first
grep -il grep * > first

if the extracting file is located in some other path then
specify the exact path

What is the error you are getting , when you use it in script. Post your script here, that would help everyone to give you the solution.

How exactly are you putting this into a script? Does the first line read #!/bin/sh and the second line contain this command, and nothing else? If not, can you try this simple script, just to set a baseline for us, and report back?

hi kamalesh,

i tried that grep command.. but can you say me what the option -il is about... actually.. what i am tying to do is that, i am formatting some file and the contents of the file i am passing it as a parameter to an sql query..

so, i will be connecting to sqlplus and i am executing the query.. the output of which i am getting in some file.. and i want to remove the last line in the sql query like "24 rows selected" etc from this file .so for that i am using grep -v option. and here i am trying to redrect the output to a file where i am stuck..

also i want to use the spool command ..to spool this output of the sql query and to create a .lst file.. where i am again stuck..:slight_smile:

thanks for the great help..

hi nua7,

thanks for the interest..

actually i am not getting any error.. when i am trying to execute the command, its creating the output file but not redirecting ...means a zero byte file will be created..

also, i am connectin to sqlplus in between.. and am getting the output of th sql query and am using the grep command to format that file... which i am unable to do..

also, can anyone help me by saying how can i spool a file.. i just know that i need to use spool on and spool off commands to do that.. but then where will i be gettnig the output .lst file..

thanks,
Krips.

hi era,

i tried using a simple shell script as you said where i just gave this much :

#!/bin/sh

grep -v 'row' /path/filename > /path/output_filename

here i am getting the output perfectly...
but in the previous shell script where i encountered the problm, i was connecting to sqlplus as well..
thern i used something like this

echo 'select STATEMENT' > sql.sql
echo 'quit' >> sql.sql

and i am sqlplus that will run this sql.sql for the particular database.. and getting the output in the specified file properly..

but after that , i was trying to remove the last line of the output of sql query like "24 rows selected" using grep command -

grep -v 'rows' $INPUT_FILE > $OUTPUT_FILE

where i am unable to get the output correctly..
can anyone help me out in finding exactly what may be the reason for this issue..

thnaks and regards,

Krips.

hi,

can i know if i am using connecting sql plus properly or not..

as of now i am giving it like

sqlplus -s user/pass@db -i sql.sql -o sql.sql.01

in the sql.sql i am having the contents like this -

select STATEMENT
quit

the output of this select statement into sql.sql.01 ...
with this am i properly exiting out of the sqlplus??

in the grep command i will be using this sql.sql.01 file as its input..
hope you all got a better idea about wat i am trying to do.. i think, i am screwing up something with my sqlplus connection...is it so??

all your sugestions are appreciated...

thanks,

Krips.

I'm guessing $INPUT_FILE is not set to what you think it's set to, or doesn't contain what you think it contains. How exactly are you generating the file, and assigning its name to a variable? Why aren't you pipiing the output through grep at the point when it is stored in a file in the first place? Or if it's always the last line you want to get rid of, try sed '$d'

hi era,

the problem was with the sqlplus connection only.. even though i was getting the correct output then also, there was something wrong..

now i am able to connect to sqlplus properly and am getting the output correctly..thanks for all your help..

but am having one doubt regarding spool command. Will we be getting a .lst file by default as output of spool command.

eg : consider that i am giving the spool command like this..

spool /path/file
SQL QUERY
spool off;

i was said by someone that if we use spool command then by default we will be getting the output in a .lst file ..is it so? please go through the link also..

hope you can clear my doubt..

thanks,

Krips.