egrep: can't open file error


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers egrep: can't open file error
# 1  
Old 07-10-2008
egrep: can't open file error

Hello folks,
I have got the following line in my script:
searchstring=".*_1234</MessageTag>"
count=`find /home/mydir/* -name "$1" | xargs egrep -hc -e $searchstring "$1" | awk '{sum+=$1};END{print sum}'`

where $1 is the name of some file that I am passing at run time.

The problem is that whenever I am running this script by just giving the name of the file instead of giving its path (like ./file1.txt) I am getting the following error:
egrep: can't open file file1.txt.

However if I am giving the path of the file it is giving me no error.
Is there any workaround for this?
Thanks a lot.
# 2  
Old 07-10-2008
>(like ./file1.txt) ...
what about just file1.txt ?
# 3  
Old 07-10-2008
Yep ... when I give it like ./file.txt it works fine. But when I give just file.txt I am getting the egrep : cannot read file file.txt. However it brings me some data alright. But obviously this is not the greatest way to do some scripting!!!
Thanks.
# 4  
Old 07-10-2008
Let's treat your command by step:
find /home/mydir/* -name "$1" | xargs egrep -hc -e $searchstring "$1"

so you looking for all files name $1 from a directory to egrep -e searchstring but in $1
deosent make sence since the output of find will not be the same as $1:
you will find strings (file with absolute path) which do contain $1 - Not the same

I would do this (Im using ksh...)
# egrep -hc -e $searchstring $(find /home/mydir/* -name "$1" | xargs )

I let you figure the rest...
# 5  
Old 07-10-2008
I agree with "vbe", but would like to explain the error message.

The error message is because the xargs generates a string containing the filename from the find and appends that string to the egrep line.

egrep -hc -e $searchstring <filename from $1> <filename from the find>

You end up searching the same file twice if both filenames are identical ... or in your case getting an error message from one of the filenames if they are different.

You need to lose the "$1" from the egrep statement, the filename from find is filled in by xargs. If "$1" does not ever contain wildcards, the find and xargs are surplus.

The example from "vbe" addresses this point.
# 6  
Old 07-11-2008
please forgive me but i am a bit of a numb skull ... if i am doing a find with an absolutely correct filename (just filename with no wild character involved) ... so i'll get the exact file i suppose ... and then i am running the egrep part ... so why do i exactly need to lose the $1 argument?
# 7  
Old 07-11-2008
Answer: Because the xargs put the filename there for you already.
The "$1" came from the original parameter used to call the script and has not been changed.
i.e. The filename is on the egrep command line twice!

xargs does not have special meaning for $1, $2 ... unlike awk. It just reads a stream of lines, removes the linebreaks, and optionally appends this string to anything else on the xargs command line.

To illustrate "xargs" in its simplest form without trying to generate a command:

cat <++++ | xargs
line1
line2
line3
++++

line1 line2 line3


To illustrate using xargs to generate a simple echo command. In the second example the filename appears twice.

myname="filename"
echo $myname|xargs echo

filename

echo $myname|xargs echo $myname

filename filename



Footnote 1: xargs has limited use on large systems for creating lists of filenames for subsequent commands because a large number of filenames can cause the generated command to malfunction if it becomes too long.

Footnote 2: It is best practice to save the parameters used to call a script (or a subroutine) into variables with meaningful names. This makes it clear to the reader whether $1, $2, $3 .... is special syntax (like in awk) or the parameters used to call the shell script.
e.g. FILENAME="${1}"
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux open failed: No such file or directory error

Hi, The below commands works fine on serverB . /etc/profile; cd /export/home/user2/utils/plugin/ ./runme.shHowever, when i run the same commands from serverA it fails $ ssh -q user2@serverB ". /etc/profile; cd /export/home/user2/utils/plugin; ./runme.sh"Output Error: Please find the below... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

egrep syntax error

I have a script that has been running fine for about a month - now all of a sudden I am getting a syntax error using the egrep command. The command I am using is comparing two files and printing only the unique lines from Second_File to Third_File. Any idea why I am getting the syntax error?? ... (4 Replies)
Discussion started by: bjdamon
4 Replies

3. UNIX for Dummies Questions & Answers

NDM: Source file open failed. Error= 2

Hi, I have try to ndm to one server to another server. but i am getting this error. what is this error? are files not in source folder? Source file open failed. Error= 2 Regards, Balamurgan (1 Reply)
Discussion started by: krbala1985
1 Replies

4. UNIX for Dummies Questions & Answers

error message when use fork with open file

I get this message when I write myshell.c program "VM pagefault:SIGSEGV bad add 0x0 err 0x4 nopage read myshell PM: ciredump signal 11 for 1725 /myshell memory fault (core dumped)" /* RCS information: $Id: myshell.c,v 1.2 2006/04/05 22:46:33 elm Exp $ */ #include <stdio.h> #include <unistd.h>... (1 Reply)
Discussion started by: rosecomp
1 Replies

5. Shell Programming and Scripting

PCC-F-NOERRFILE, unable to open error message file

Hi, I was compiling few C programs in the unix server and getting the following error message rm: /home/a0xxx28/AVT/SEEDLIBRARYDB/LIB/*.a non-existent proc MODE=ANSI CODE=ANSI_C INCLUDE=/home/a0xxx28/PVT/SEEDLIBRARYDB/INCLUDE INCLUDE=/home/a0xxx28/PVT/SEEDLIBRARY/INCLUDE ... (1 Reply)
Discussion started by: kavithakuttyk
1 Replies

6. Solaris

Error- ld.so.1: expr: fatal: libgmp.so.3: open failed:No such file or directory

Hi Friends I have a compiler(Sun Forte,I believe) running in my Solaris 9 box. since y'day my development team is finding this error when they compile: ld.so.1: expr: fatal: libgmp.so.3: open failed: No such file or directory I ran a search for this file and found it in one of my file... (2 Replies)
Discussion started by: Hari_Ganesh
2 Replies

7. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

8. Shell Programming and Scripting

Failed to open output file Error

Hi guys, I Have written a script,In that it will call another file which contains the sql quaries. while wxecuting that I am getting the below exception 01/16|06:28:06:16800: Operating System Error|Failed to open output file Can anybody help me about this,,Its urgent (0 Replies)
Discussion started by: Anji
0 Replies

9. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies

10. Solaris

Error Message: find: cannot open /: Stale NFS file handle

I am using the "find" command below and it respond with the error message " find: cannot open /: Stale NFS file handle" what does it mean? what can I do about it? Need advice, thanks. below are the command that I enter, the respond and the error message: root@ScripServer:/# find / -name... (1 Reply)
Discussion started by: ezsurf
1 Replies
Login or Register to Ask a Question