LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2016, 08:05 PM   #16
susja
Member
 
Registered: May 2011
Posts: 125

Original Poster
Rep: Reputation: 1

find . -iname '*.zip' -print0 | while read -d '' -r file; do
mkdir temp
unzip -qq "$file" -d temp
grep -irl DNA0000005619_H002 temp/*
rm -fr temp
done
--
./test2.sh
warning: ./Backup files 1.zip appears to use backslashes as path separators
fchmod (file attributes) error: Permission denied
fchmod (file attributes) error: Permission denied
fchmod (file attributes) error: Permission denied
fchmod (file attributes) error: Permission denied
fchmod (file attributes) error: Permission denied
temp/C/ProgramData/Tecan/EVOware/output/Sample Tracking/Archive/RePf000000001.csv
[TBD]$
--

Great! It found correct file. The last thing left is somehow get rid of 'warning' and other 'irrelevant' stuff. I need to see only file and nothing more ..

Thanks
 
Old 06-22-2016, 09:37 PM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Seeing as no one else has mentioned it, please use [code][/code] tags around your code so it is more readable and maintains formatting.

"irrelevant" is a stretch .. it is giving you information that may be necessary, but if you really want to get rid of it you can redirect the stderr from your loop to go to /dev/null if you feel the
information is not warranted.
 
Old 06-22-2016, 10:04 PM   #18
susja
Member
 
Registered: May 2011
Posts: 125

Original Poster
Rep: Reputation: 1
Code:
find . -iname '*.zip' -print0 | while read -d '' -r file; do
mkdir temp
unzip -qq "$file" -d temp
grep -irl DNA0000005619_H002 temp/*
rm -fr temp
done
I'm going to 'SOLVE' it .. but could you point me which place should I redirect stderr to /dev/null ? like 2> /dev/null where to put it?
Thanks
 
Old 06-22-2016, 11:05 PM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
After 'done'
 
Old 06-23-2016, 12:58 AM   #20
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
try to put it after done:
Code:
find . .... | while read ... do
...
...
...
done 2>/dev/null
 
Old 06-23-2016, 09:36 AM   #21
susja
Member
 
Registered: May 2011
Posts: 125

Original Poster
Rep: Reputation: 1
Folks,
Now everything works as expected. Thanks for the help.
The last thing still missing is the 'path' to the file
I have an output like this:
temp/C/ProgramData/Tecan/EVOware/output/..../file.x
But this does not help me. I need 'absolute' path to the file like
ABC.zip/C/ProgramData...
or
DEF.zip/C/....
How script could be modified to get the whole path?
Thanks again ..
 
Old 06-23-2016, 10:13 AM   #22
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Try this:
Code:
#!/bin/bash

find . -iname '*.zip' -print0 | while read -d '' -r file; do
  mkdir temp
  unzip -qq "$file" -d temp
  grep -irl DNA0000005619_H002 temp/* | awk -F\n '{print gensub("^temp","'"$file"'",1,$1)}'
  rm -fr temp
done
 
Old 06-23-2016, 11:20 AM   #23
susja
Member
 
Registered: May 2011
Posts: 125

Original Poster
Rep: Reputation: 1
- suicidaleggroll

It did not make any difference.
I still get this:
/search.sh DNA0000005631
temp/C/ProgramData/Tecan/EVOware/output/Sample Tracking/.../file.x
temp/C/ProgramData/Tecan/EVOware/output/Sample Tracking/.../file.c
 
Old 06-23-2016, 11:25 AM   #24
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Are you sure? That shouldn't be possible and I tested it on my own system before posting.

Please copy-paste the code and output from running it.
 
Old 06-23-2016, 11:40 AM   #25
susja
Member
 
Registered: May 2011
Posts: 125

Original Poster
Rep: Reputation: 1
cat ./search.sh
#!/bin/bash

## This script will search for provided PATTERN in 'zipped' files and list files
## For better performance go to the 'lowest' directory
## Usage: ./search.sh <PATTERN> | sort | uniq

/usr/bin/find . -iname '*.zip' -print0 | while read -d '' -r file; do
mkdir temp
/usr/bin/unzip -qq "$file" -d temp | awk -F\n '{print gensub("^temp","'"$file"'",1,$1)'
grep -irl $1 temp/*
rm -fr temp
done 2>/dev/null
---
./search.sh DNA0000005631|sort|uniq
temp/C/ProgramData/Tecan/EVOware/output/Sample Tracking/Archive/DVALABB002419_04282016_131616.csv
temp/C/ProgramData/Tecan/EVOware/output/Sample Tracking/Archive/DVALABB002420_04282016_131616.csv
temp/C/ProgramData/Tecan/EVOware/output/Sample Tracking/Reports/NormalizeReport04282016_131615.csv
sh-4.3$


Thanks again
 
Old 06-23-2016, 11:48 AM   #26
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You added the awk to the output of unzip instead of grep and you missed the '}'.

Last edited by suicidaleggroll; 06-23-2016 at 11:50 AM.
 
Old 06-23-2016, 12:57 PM   #27
susja
Member
 
Registered: May 2011
Posts: 125

Original Poster
Rep: Reputation: 1
Well ... sorry for my typo
Now it produce correct path but it's truncated and does not give me the name of the file I'm interested in ..
I have this:
./search.sh DNA0000005631
./Backup Files 2016-05-26 142101/Backup files 1.zip/C/ProgramData/Teca
While the path is:
temp/C/ProgramData/Tecan/EVOware/output/Sample Tracking/Archive/DVALABB002419_04282016_131616.csv
Having truncated path does not allow me to locate the file
 
Old 06-23-2016, 01:24 PM   #28
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
heh, my test case apparently didn't have any N's in the path to the file!

Try this awk instead:
Code:
awk '{print gensub("^temp","'"$file"'",1)}'
 
Old 06-23-2016, 01:47 PM   #29
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe terminal output is seen through some sort of pager that truncates long lines? Just a thought
 
Old 06-23-2016, 02:03 PM   #30
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by keefaz View Post
Maybe terminal output is seen through some sort of pager that truncates long lines? Just a thought
Nah, it's the field separator I used in the awk. I was trying to effectively disable it by setting it to a new line, but apparently "-F\n" just sets it to a literal "n", so the first "n" it ran into it split the line. I just switched the gensub to use the entire input "$0" and disabled field splitting, which should take care of it.
 
1 members found this post helpful.
  


Reply

Tags
find, grep



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find files using grep and find Nitron Linux - Newbie 11 10-04-2015 11:59 AM
[SOLVED] grep: find files that do not have multiple different strings kmkocot Linux - Newbie 4 04-22-2015 12:55 PM
[SOLVED] how to use cp find and grep together to copy a list of files using find with grep babhijit Linux - Newbie 10 07-03-2013 12:25 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
searching within files. cat grep find w3bd3vil Linux - Newbie 4 10-13-2008 03:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration