find grep whitespace problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find grep whitespace problem
# 1  
Old 05-30-2009
find grep whitespace problem

Hey guys iv written a basic function to ask for input location to find then grep a certain string in the file at the location of the find.
For some reason it finds the file with the certain string it however it says directory not found on each string before the whitespace

eg enter location to search
/home/s/steves/
enter string to grep in /home/s/steves/
hello world

/home/s/steves/myfile.txt hello world
/usr/bin/local/grep/ hello no such file or directory found
/usr/bin/local/grep/ world no such file or directory found
Does anyone no any reason i tried dealing with the white space below but not sure if its correct


$FIND "$path" -type f | $AWK -F "\n" '{ print "\"" $1 "\"" } ' | $XARGS $GREP $searchString

Last edited by musicmancanora4; 05-30-2009 at 11:23 PM..
# 2  
Old 05-30-2009
you will need to quote the search string.

grep "hello world"
# 3  
Old 05-30-2009
Hi thanks for your fast response is there a way where i dont have to insert ""?
# 4  
Old 05-31-2009
you can put the quotes in the code
Code:
$FIND "$path" -type f | $AWK -F "\n" '{ print "\"" $1 "\"" } ' | $XARGS $GREP "$searchString"

# 5  
Old 05-31-2009
Hi thanks i forgot to do that it now works Smilie

Just another quick question
to make my code more portable im declaring functions like this at the start my program

eg

READ=/usr/bin/read

i do a which read and it says thats where it is located

however the read does not work it does not proceed with asking for user input when the code is run if i call it using $READ variableName

i use GREP=/usr/local/bin/grep and call it with $GREP that works
but if i use $read lowercase it works any idea's?
# 6  
Old 05-31-2009
most often uppercase names are reserved for the OS.
so in your OS you may find that $READ is already need by the OS.

as for making your code more portable...
I would just set your path at the start of the script, then just use commands directly.

Code:
#!/bin/sh
PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin; export PATH

find "$path" -type f | awk -F "\n" '{ print "\"" $1 "\"" } ' | $XARGS grep "$searchString"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

2. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

3. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

4. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

5. Shell Programming and Scripting

Cut and WhiteSpace Problem

Hi, I have a shell script that reads a parameter file to set variables. I have an issue when the parameter I try to read contains whitespace. e.g File Contents Code The result is SUBJECT is set to and I want subject set to I've tried different variations but nothing seems to... (19 Replies)
Discussion started by: Greygor
19 Replies

6. UNIX for Advanced & Expert Users

whitespace problem

I have a single string as below: Rat run after Cat i.e. there is a single whitespace after Cat. This causes my file to fail. Is there a way I can remove any whitespace at the end of any string. I tried sed 's/ *//g', but it removes all white space and the above string becomes... (10 Replies)
Discussion started by: RubinPat
10 Replies

7. Shell Programming and Scripting

Copy files listed in a text file - whitespace problem.

Hi, Say I have this text file <copy.out> that contains a list of files/directories to be copied out to a different location. $ more copy.out dir1/file1 dir1/file2 dir1/file3 "dir1/white space" dir1/file4 If I do the following: $copy=`more copy.out` $echo $copy dir1/file1... (4 Replies)
Discussion started by: 60doses
4 Replies

8. Shell Programming and Scripting

grep is not working, please find out the problem

HI Friends, Traxlist.txt --this is my file contains this txt ------------- 108042708190000.txt 108042708190001.txt 112040108174516.txt 112042708173000.txt 112042708174508.txt 112050108173000.txt 113042708100500.txt 113042708103026.txt i have varible called... (3 Replies)
Discussion started by: kittusri9
3 Replies

9. UNIX for Advanced & Expert Users

find columns with whitespace as field seperator?

Hai I am using bash-2.03$ bash --version GNU bash, version 2.03.0(1)-release (sparc-sun-solaris) I am not able to use gawk command its showing command not found , why ? Eg: awk 'NR==1' fix.txt | gawk 'BEGIN { FIELDWIDTHS = "3 2" } { printf($1"|"$2); }'... (8 Replies)
Discussion started by: tkbharani
8 Replies

10. UNIX for Dummies Questions & Answers

FIND/GREP problem

When I enter the command below grep appears to be returning a file it shouldn't. find . -name "*.*" -exec grep "testing" {} /dev/null \; .:ops3Mailfile ./SSI.ksh: # create TECHOUT dummy for test for testing purposes ./ftprimi1.ksh:# before running job in prod... change FTP to... (1 Reply)
Discussion started by: dfb500
1 Replies
Login or Register to Ask a Question