LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-04-2021, 01:43 AM   #31
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120

Great way to learn is to study the different ways people solve things. You will learn something new every day.
 
Old 03-04-2021, 01:52 AM   #32
Alok Rai
Member
 
Registered: Aug 2015
Posts: 245

Original Poster
Rep: Reputation: Disabled
Thanks. I'm going to try and learn something about Bash scripts, and then return to this.
 
Old 03-04-2021, 08:47 AM   #33
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
Quote:
Originally Posted by Alok Rai View Post
This is waaaaaaay beyond my range, but thanks a ton! I'll come back when I am big and strong....
Let's see. I'll try to provide an explanation of what's happening, but since I'm not an expert there'll be things I get wrong and some things I can't explain at all. I hope someone will help out then!

The code, once again:
Code:
#!/bin/bash
# script name: odf-grep.sh
# usage: from the appropriate folder, run "odf-grep.sh search_string"
[ "$1" ] || { echo "You forgot the search string!" & exit; }
find . -type f -name "*.od*" | while read -r i ; do
  unzip -c "$i" | grep -iq "$1" && echo "string found in $i"
done
As an example, I have a bunch of .ods and .odt files in ~/Dropbox/CurrSoc/MCM/old and I want to know which .ods/.odt files have the phrase "sinking fund". So, in the terminal, I navigate to the relevant folder and run odf-grep.sh "sinking fund". This is the output:
Code:
string found in ./IE201605May.ods
string found in ./2018-May-mcm-agenda.odt
string found in ./20150329-mcm-agenda-draft.odt
(Note that I have odf-grep.sh located in ~/bin which is recognized by the system as a location for executables. Check your set-up by running echo $PATH.)

What it does:
[ "$1" ] || { echo "You forgot the search string!" & exit; }
At the outset, the script checks whether you have provided the search string or not. If you haven't provided a search term, the script exits.
[ "$1" ] is shorthand for testing whether the search term is provided or not. See https://www.lifewire.com/test-linux-...ommand-4097166 for a simple explanation about tests in Bash and the use of "||" and "&&" both of which are used in this script.
In this instance, "$1" refers to the search string. (I think the technical term is "positional parameter".)
If it's not present the test fails, a message is echoed to the screen and the script exits.
If a search string is provided, the rest of the script kicks in.

find . -type f -name "*.od*"
"." indicates that find should start in the current folder and work down (recurse) into subfolders
"-type f" specifies that files are to be searched
"-name" precedes the filenames to be searched; in this case, we're looking at any open document format files.
You can run find . -type f -name "*.od*" separately just to see what the output of find looks like.
| while read -r i ; do
"|" takes the output of the command on the left and passes or pipes it to another operation, in this case a while loop
In imprecise terms, for each file found by find and represented by "i", we're asking for something to be done
unzip -c "$i"
This is simple: the target file is unzipped (but the contents aren't stored on your system unlike using unzip without -c )
| grep -iq "$1"
the contents of the unzip operations are piped to the grep command which searches the contents for the presence of the specified string, the "$1". The search is case-insensitive (because of "-i") and only communicates whether the term was found or not because of "-q" via grep's exit status
&& echo "string found in $i"
If the preceding grep operation was successful (= the *.od* file contains the search string) because of && (see the lifewire link for && and ||), output a message with the file's relative path and name.
done
balances the "do" of the while loop
 
1 members found this post helpful.
Old 03-04-2021, 11:24 AM   #34
Alok Rai
Member
 
Registered: Aug 2015
Posts: 245

Original Poster
Rep: Reputation: Disabled
Dear Hish2021,

This is invaluable - it is a veritable education! I can't begin to tell you how grateful I am for the trouble you have taken. It will take me a little time to work my way through this - and I might still have some questions after I have - but I can recall formal courses, where I've been taught less - and certainly less meticulously.

Thanks, again,

Alok Rai
 
Old 03-04-2021, 11:27 AM   #35
Alok Rai
Member
 
Registered: Aug 2015
Posts: 245

Original Poster
Rep: Reputation: Disabled
In the example you've given, hish2021 - at what stage in the script do you insert the particular search string "sinking fund"?

I haven't had a chance, yet, to work through your mail - it's late here, getting to midnight - so perhaps I should wait until I have done my homework.

Thanks.
 
Old 03-04-2021, 07:22 PM   #36
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
Let's say you want to look through files LibreOffice Writer (.odt) or LibreOffice Calc (.ods) files located in ~/Documents for the phrase "sinking fund". In your terminal move to ~/Documents by running
cd ~/Documents
Your terminal prompt should change to reflect the new "current working directory".
Then run
Code:
odf-grep.sh "sinking fund"
 
Old 03-05-2021, 12:13 AM   #37
Alok Rai
Member
 
Registered: Aug 2015
Posts: 245

Original Poster
Rep: Reputation: Disabled
This is the output produced by $PATH -

$PATH
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory


I can't see a bin directory in my /home/alok directory. Here is the ls output for it -

'Calibre Library' Documents mozilla.pdf Public
CLAIRE_FICTION Downloads Music Templates
Desktop frank_ramsey.pdf Pictures Videos

The /home directory has only alok.

And the root directory, finally, has the directories that might be relevant -

bin dev initrd.img lost+found opt run swapfile usr
boot etc initrd.img.old media proc sbin sys var
cdrom home lib mnt root srv tmp vmlinuz


Does this have a bearing on where my odf-grep.sh script goes?
 
Old 03-05-2021, 02:52 AM   #38
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
Quote:
Originally Posted by Alok Rai View Post
This is the output produced by $PATH -

$PATH
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory
...
Does this have a bearing on where my odf-grep.sh script goes?
Mint maybe doing things differently than Ubuntu.

1: Could you post the output of cat ~/.bash_profile here?
2: You can have your scripts anywhere you like but if they're not in PATH, you'll need to reference the entire path each time you want to run them (or make an alias that includes the path).
3: And, of course, the script should be made executable, either via your File Manager > Properties > Permissions or by running chmod +x odf-grep.sh in your terminal where your script is located.
 
Old 03-05-2021, 04:21 AM   #39
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by hish2021 View Post
the script should be made executable
Yes, it's more convieniet this way, but not necessarily, bash odf-grep.sh "sinking fund" would do as well.
 
Old 03-05-2021, 05:00 AM   #40
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
... bash odf-grep.sh "sinking fund" would do as well.
That's neat and also allows for running from a folder that's not listed in $PATH.
 
Old 03-05-2021, 05:16 AM   #41
Alok Rai
Member
 
Registered: Aug 2015
Posts: 245

Original Poster
Rep: Reputation: Disabled
Not much luck with the cat ~/.bash_profile - alas, it reports "no such file or directory". Not sure where to look for the .bash_profile.

Meanwhile, running it with the additional bash at the beginning, as suggested by shruggy, would be a workable option - bash odf-grep.sh "sinking fund

Thank you - am learning, learning...
 
Old 03-05-2021, 06:10 AM   #42
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
Quote:
Originally Posted by Alok Rai View Post
Not much luck with the cat ~/.bash_profile - alas, it reports "no such file or directory". Not sure where to look for the .bash_profile.

Meanwhile, running it with the additional bash at the beginning, as suggested by shruggy, would be a workable option - bash odf-grep.sh "sinking fund

Thank you - am learning, learning...
Could you try just cat ~/.profile?
 
Old 03-05-2021, 11:43 AM   #43
Alok Rai
Member
 
Registered: Aug 2015
Posts: 245

Original Poster
Rep: Reputation: Disabled
Success! Here is what cat ~/.profile threw up -

$ cat ~/.profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
 
Old 03-05-2021, 10:10 PM   #44
hish2021
Member
 
Registered: Jan 2021
Posts: 117

Rep: Reputation: Disabled
Quote:
Originally Posted by Alok Rai View Post
Success! Here is what cat ~/.profile threw up -
Code:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
Great. So look at what I've quoted from your .profile. It just means that if you create ~/bin and/or ~/.local/bin, both those folders will be added to your $PATH and that executable scripts placed there can be used like any other system command (without you having to provide the path).

A log out and login maybe needed after you create ~/bin and/or ~/.local/bin. I have just ~/bin.

(You may get away by just "sourcing" ~/.profile after you've created the folder instead of rebooting. I don't know for sure.)

-----

BTW, this forum prefers that content copied from your terminal and pasted here be enclosed in "code" tags. Highlight the pasted stuff and then click the "#" icon above the posting area.

Last edited by hish2021; 03-05-2021 at 10:13 PM.
 
Old 03-05-2021, 11:27 PM   #45
Alok Rai
Member
 
Registered: Aug 2015
Posts: 245

Original Poster
Rep: Reputation: Disabled
Great. Thanks. This has been invaluable.
 
  


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:19 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