Link to home
Start Free TrialLog in
Avatar of Vijay kumar Mohanraj
Vijay kumar MohanrajFlag for Malaysia

asked on

grep and find the string filename including null spaces

Hi Experts,

OS:REDHAT

How to grep the string " *.xsl " from a  .log file, then find that string named  file throughout ./Documents  folders and sub folders. And store those files in a separate folder(extractedfiles).
Some filenames also includes null spaces.
Commands would be more preferable as it is easy to execute right away without keeping my hand in the permissions..

Code:
for file in $(grep -rhZ ".xls"  ./thelogfiles/) ; do find ./Documents/ -iname "$file" -type f -print0 | xargs -0 -i cp '{}' ./extractedfiles ; done

So now, iam getting the output in "extractedfiles" folder. Except for the null spaces filename Ex:this that.xls.
Want "this that.xls" type of files also to be copied to ./extractedfiles
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Should be simple:

for file in "$(grep -rhZ ".xls"  ./thelogfiles/)" ; do find ./Documents/ -iname "$file" -type f -print0 | xargs -0 -i cp '{}' ./extractedfiles ; done

Note the quotes ( "  " ) around the $(grep .. ) expression!

wmp
Avatar of Vijay kumar Mohanraj

ASKER

woolmilkporc:

no its not working, the null space filenames are not getting copied to the ./extractedfiles
I tested with spaces in filenames, and it works for me.

What do you mean then with "null space"? Space? Or Null? Or both?
woolmilkporc:

Because of placing  quotes ( "  " ),  no files are copied to the output folder ./extractedfiles
(Atleast it copied the filename  "without spaces" before)

for file in "$(grep -rhZ ".xls"  ./thelogfiles/)" ; do find ./Documents/ -iname "$file" -type f -print0 | xargs -0 -i cp '{}' ./extractedfiles ; done

woolmilkporc:


 filename like Ex:this that.xls.(files containing space)

Want "this that.xls" type of files also to be copied to ./extractedfiles
I just saw I did my tests with single quotes around '.xls'. Not sure if this is the reason - can't repeat my tests right now, sorry!

for file in "$(grep -rhZ '.xls'  ./thelogfiles/)" ; do find ./Documents/ -iname "$file" -type f -print0 | xargs -0 -i cp '{}' ./extractedfiles ; done
OK,

I see the problem now. I didn't have the time to set up a big test environment, so I used only one file - and that was misleading, because my version works with one file, but not with many.

Try this:

grep -rhZ '.xls'  ./thelogfiles/ | while read file ; do find ./Documents/ -iname "$file" -type f -print0 | xargs -0 -i cp '{}' ./extractedfiles ; done
woolmilkporc:

nope, sorry

Because of placing  quotes ( "  " ),  no files are copied to the output folder ./extractedfiles
(Atleast it copied the filename  "without spaces" before)


Tried this,
for file in "$(grep -rhZ '.xls'  ./thelogfiles/)" ; do find ./Documents/ -iname "$file" -type f -print0 | xargs -0 -i cp '{}' ./extractedfiles ; done

woolmilkporc:

Tried this,
for file in "$(grep -rhZ '.xls'  ./thelogfiles/)" ; do find ./Documents/ -iname "$file" -type f -print0 | xargs -0 -i cp '{}' ./extractedfiles ; done

Its working, but another issue have been raised,
 inside ./thelogfiles, i have .log files, so we are trying to search all string name  containing " *.xls" and find it as filename in ./Documents

So now, all files like
123.xls
2_3_4.xls
2 4.xls
are copied to the ./extractedfiles/

But, in certain .log files string name is like this..

file name   123.xls
file name    567.xls

these types of files are not copied to the ./extractedfiles/
The command in your last post is not what I suggested in my last post.
woolmilkporc:

sorry tried this,

grep -rhZ '.xls'  ./thelogfiles/ | while read file ; do find ./Documents/ -iname "$file" -type f -print0 | xargs -0 -i cp '{}' ./extractedfiles ; done

Its working, but another issue have been raised,
 inside ./thelogfiles, i have .log files, so we are trying to search all string name  containing " *.xls" and find it as filename in ./Documents

So now, all files like
123.xls
2_3_4.xls
2 4.xls
are copied to the ./extractedfiles/

But, in certain .log files string name is like this..

file name   123.xls
file name    567.xls

these types of files are not copied to the ./extractedfiles/
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
woolmilkporc:

Just worked like a charm,
Can you explain how adding awk  and "*${file}*" made the code work...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial