Vijay kumar Mohanraj
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
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
ASKER
woolmilkporc:
no its not working, the null space filenames are not getting copied to the ./extractedfiles
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?
What do you mean then with "null space"? Space? Or Null? Or both?
ASKER
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
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
ASKER
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
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
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
ASKER
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
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
ASKER
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.
ASKER
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/
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
woolmilkporc:
Just worked like a charm,
Can you explain how adding awk and "*${file}*" made the code work...
Just worked like a charm,
Can you explain how adding awk and "*${file}*" made the code work...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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