LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-02-2021, 04:57 AM   #16
Alok Rai
Member
 
Registered: Aug 2015
Posts: 247

Original Poster
Rep: Reputation: Disabled

Thanks, shruggy!

So, at the terminal prompt, I would run this command -

zipgrep pattern "$f" documents/*.odt

And then I would get a list that I could pipe to "more"????

Is that what "invoke it in a loop" means? Sorry if I am being dense, but please bear with me..

Thanks.
 
Old 03-02-2021, 05:13 AM   #17
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
Would zipgrep be more convenient than the script mentioned in posts #5 and #6 if your usage is what was described in the original post?
 
Old 03-02-2021, 05:13 AM   #18
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Quote:
Originally Posted by Alok Rai View Post
So, at the terminal prompt, I would run this command
No. At the terminal prompt you can either use the for loop as shown above
Code:
for f in documents/*.odt;do zipgrep pattern "$f";done
or find
Code:
find documents/ -type f -name \*.odt -exec zipgrep pattern {} \;
Another possibility are grep-like tools that allow both searching inside archives and specifying multiple input files/directories, e.g. ugrep:
Code:
ug -z pattern documents/*.odt

Last edited by shruggy; 03-02-2021 at 05:26 AM.
 
Old 03-02-2021, 10:42 PM   #19
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,365

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
According to the manpage https://linux.die.net/man/1/zipgrep, it offers all the usual egrep options https://linux.die.net/man/1/egrep, which of course include -r (recursive ) ....
 
Old 03-03-2021, 02:05 AM   #20
Alok Rai
Member
 
Registered: Aug 2015
Posts: 247

Original Poster
Rep: Reputation: Disabled
Thanks, Chris! That should do me fine!
 
Old 03-03-2021, 04:50 AM   #21
Alok Rai
Member
 
Registered: Aug 2015
Posts: 247

Original Poster
Rep: Reputation: Disabled
Ah well - spoke too soon!

I tried using zipgrep. Here is the command - and the output -

$ zipgrep pattern "Hindutva" *.odt
zipinfo: cannot find or open Hindutva, Hindutva.zip or Hindutva.ZIP.
/usr/bin/zipgrep: 97: test: -eq: unexpected operator
/usr/bin/zipgrep: 100: test: Illegal number:


I am in the directory Documents - and wish to search therein.

Help!
 
Old 03-03-2021, 04:54 AM   #22
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
pattern is not a command word, it stays for the pattern itself
Code:
zipgrep "Hindutva" file.odt
 
Old 03-03-2021, 05:01 AM   #23
Alok Rai
Member
 
Registered: Aug 2015
Posts: 247

Original Poster
Rep: Reputation: Disabled
Now, this:

$ zipgrep "Hindutva" Documents/*.odt
zipinfo: cannot find or open Documents/*.odt, Documents/*.odt.zip or Documents/*.odt.ZIP.

No zipfiles found.
/usr/bin/zipgrep: 97: test: -eq: unexpected operator
/usr/bin/zipgrep: 100: test: Illegal number:
 
Old 03-03-2021, 05:03 AM   #24
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Sure, because you can specify only one file as an argument to zipgrep. Besides, if your current directory is already ~/Documents then specifying Documents/*.odt is wrong.

I agree with what hish2021 said above, I also think a Bash script as suggested in #8 would serve you better.

Last edited by shruggy; 03-03-2021 at 05:50 AM.
 
Old 03-03-2021, 05:19 AM   #25
Alok Rai
Member
 
Registered: Aug 2015
Posts: 247

Original Poster
Rep: Reputation: Disabled
Tried the command on a different directory - and managed to get some results. But am now even more puzzled - because it threw up filenames where the body of the files contains nary a mention of "Hindutva" - for instance, this - from a file named alice_impossible -

Alice laughed. “There’s no use trying,” she said: “one can’t believe impossible things.”
“I daresay you haven’t had much practice,” said the Queen. “When I was your age, I always did it for half-an-hour a day. Why, sometimes I’ve believed as many as six impossible things before breakfast.”
[Carroll, Through the Looking Glass, chapter 5.]


Weird, isn't it?
 
Old 03-03-2021, 06:05 AM   #26
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
zipgrep treats an .odt file as a zip archive. It will search not only in the document text, but in the metadata, too. E.g. if the document was created by user Hindutva it will be matched.
 
Old 03-03-2021, 07:07 AM   #27
Alok Rai
Member
 
Registered: Aug 2015
Posts: 247

Original Poster
Rep: Reputation: Disabled
Brilliant, thank you! So, now if I can find a way of accessing the metadata, and manipulating that, I can, in effect, attach keywords to documents!

So much to learn, thank you!
 
Old 03-03-2021, 07:27 AM   #28
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
You can manipulate LibreOffice document metadata with unoconv
Code:
unoconv -f odt -o output.odt -M Author="Alok Rai" input.odt

Last edited by shruggy; 03-03-2021 at 07:38 AM.
 
Old 03-03-2021, 08:04 AM   #29
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
I tried to understand the code posted in #6

Code:
#!/bin/bash

find . -type f -name "*.od*" | while read i ; do
[ "$1" ] || { echo "You forgot search string!" ; exit 1 ; }
unzip -ca "$i" 2>/dev/null | grep -iq "$*"
if [ $? -eq 0 ] ; then
echo "string found in $i" | nl
fi
done
In the process, I changed things to this:

Code:
#!/bin/bash

[ "$1" ] || { echo "You forgot the search string!" & exit; }
find ~/Documents -type f -name "*.od*" | while read -r i ; do
  unzip -c "$i" | grep -iq "$1" && echo "string found in $i"
done
I first had just "while read i" but https://www.shellcheck.net/ suggests using "read -r" (https://github.com/koalaman/shellcheck/wiki/SC2162).
 
Old 03-04-2021, 12:10 AM   #30
Alok Rai
Member
 
Registered: Aug 2015
Posts: 247

Original Poster
Rep: Reputation: Disabled
This is waaaaaaay beyond my range, but thanks a ton! I'll come back when I am big and strong....
 
  


Reply



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
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
[SOLVED] Search for a word in multiple ODT files NotAComputerGuy Linux - Software 6 05-10-2016 12:26 PM
[SOLVED] Problem opening odt document in LibreOffice Subhraman Sarkar Linux - General 8 01-27-2015 08:36 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
find string in filename and use string to create directories daberkow Linux - Newbie 11 05-01-2009 02:12 PM

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

All times are GMT -5. The time now is 06:24 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