Find and replace

Questions about applications and software
Forum rules
Before you post please read how to get help
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Find and replace

Post by Klaas Vaak »

I sometimes need to find in multiple files a word, or sentence, and replace it/them with another. I have been looking for a good app that can do this but have not found one. I did come across "Find and Replace" (FAR), which would fit the bill, except for Linux you have to download a .tar.xz file and build the app, which is beyond my capabilities and interest.

A number of times I found the terminal command below, but that only seems to apply to 1 word at the time:

Code: Select all

grep -lr -e '<oldword>' * | xargs sed -i 's/<oldword>/<newword>/g'
or is it possible to put between the apostrophes multiple words/a sentence?

Any ideas for a good app? I would prefer that.
Last edited by Klaas Vaak on Mon Sep 23, 2019 7:57 am, edited 2 times in total.
User avatar
catweazel
Level 19
Level 19
Posts: 9884
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Find and replace

Post by catweazel »

Klaas Vaak wrote:
Fri Sep 20, 2019 3:58 am
Any ideas for a good app? I would prefer that.
I use a shell script for that.

Code: Select all

#!/bin/bash
for word in $( cat /path.to/list.of.words ) ; do
    rename "s/$word//g" *
done
list.of.words:

Code: Select all

able
baker
\ charlie
\ dog
echo
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
ColdBootII
Level 5
Level 5
Posts: 696
Joined: Wed Aug 02, 2017 8:19 am

Re: Find and replace

Post by ColdBootII »

Klaas Vaak wrote:
Fri Sep 20, 2019 3:58 am
I except for Linux you have to download a .tar.xz file and build the app, which is beyond my capabilities and interest.
Not exactly... Extract downloaded archive and give execute permission to far.sh to run the app.
donalduck
Level 3
Level 3
Posts: 185
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: Find and replace

Post by donalduck »

another possible command:

Code: Select all

find . -type f -iname "*.txt" -exec sed -i "s/oldword1/newword1/g;s/oldword2/newword2/g" {} \;
dixit info sed:
There are several methods to specify multiple commands in a 'sed' program.

Using newlines is most natural when running a sed script from a file (using the '-f' option).

On the command line, all 'sed' commands may be separated by newlines.
Alternatively, you may specify each command as an argument to an '-e' option:

$ seq 6 | sed '1d
3d
5d'
2
4
6

$ seq 6 | sed -e 1d -e 3d -e 5d
2
4
6

A semicolon (';') may be used to separate most simple commands:

$ seq 6 | sed '1d;3d;5d'
2
4
6
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: Find and replace

Post by Klaas Vaak »

catweazel wrote:
Fri Sep 20, 2019 4:27 am
I use a shell script for that.

Code: Select all

#!/bin/bash
for word in $( cat /path.to/list.of.words ) ; do
    rename "s/$word//g" *
done
list.of.words:

Code: Select all

able
baker
\ charlie
\ dog
echo
@catweazel: excuse my ignorance, but should that list.of.words be in the same script or is it a separate script that the rename script looks into when it executes its code? I ask because you put it in a different code box.
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: Find and replace

Post by Klaas Vaak »

ColdBootII wrote:
Fri Sep 20, 2019 7:03 am
Not exactly... Extract downloaded archive and give execute permission to far.sh to run the app.
@ColdBootll: yes, that works. Thanks for pointing it out.
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: Find and replace

Post by Klaas Vaak »

donalduck wrote:
Fri Sep 20, 2019 7:59 am
another possible command:

Code: Select all

find . -type f -iname "*.txt" -exec sed -i "s/oldword1/newword1/g;s/oldword2/newword2/g" {} \;
@donalduck: so, if I understand well, one can just add as many old words and new words to that command as one likes.
donalduck
Level 3
Level 3
Posts: 185
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: [SOLVED] Find and replace

Post by donalduck »

as many old words and new words to that command as one likes
not as many but a lot for sure. there are others limitations !
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: [SOLVED] Find and replace

Post by Klaas Vaak »

Thanks for your reply @ donalduck, I did not think anybody was going to reply anymore.

I have some markdown files with a path to a folder as part of the text.

How would I replace say
media/veracrypt1/Geopolitics/images/

with say
media/veracrypt1/Geopolitics/

i.e. leave off the "images/" bit?
donalduck
Level 3
Level 3
Posts: 185
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: Find and replace

Post by donalduck »

OK I don't know if you are fluent with sed syntax, but to be very specific I'll show you 2 solutions with sed:

Code: Select all

sed "s/media\/veracrypt1\/Geopolitics\/images\//media\/veracrypt1\/Geopolitics\//g" my-file

Code: Select all

sed "s/\(media\/veracrypt1\/Geopolitics\/\)images\//\1/g"  my-file
what you should remember is that you need to protect special characters with \
and that you can mark expressions with ( ) and re-use them \1 \2 ...
and also that sed might have some problems with very long lines.

last but not least it is a bad idea to use sed -i option if you have no backups.
User avatar
phd21
Level 19
Level 19
Posts: 9673
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Find and replace

Post by phd21 »

Hi "Klaas Vaak",

I just read your post and the good replies to it. Here are my thoughts on this as well.

FYI: There are also desktop GUI applications for search (find) almost anything in any file like "Recoll", "DocFetcher", "FSearch", and "SearchMonkey", etc... for searching multiple files. For searching and replacing within a single document or text file, you can use almost any word processor like LibreOffice Writer or almost any text editor like "Kate", "Gedit", "Xed", Leafpad, etc... or programming editors. Perhaps "KFileReplace", "regexxer", etc... Desktop apps or "RPL" console terminal command for searching and replacing within multiple files.

[SOLVED] Spotlight (or Windows search) equivalent - Linux Mint Forums
viewtopic.php?f=90&t=288901&hilit=searchmonkey

Help Installing DocFetcher - Linux Mint Forums
viewtopic.php?f=47&t=286373&hilit=searchmonkey

RPL (rpl) console terminal command - install from "Synaptic Package Manager (SPM)".
rpl --help
Usage: rpl [options] old_string new_string target_file(s)

Options:
--version show program's version number and exit
-h, --help show this help message and exit
-L, --license show the software license
-x SUFFIX specify file suffix to match
-i, --ignore-case do a case insensitive match
-w, --whole-words whole words (old_string matches on word boundaries only)
-b, --backup make a backup before overwriting files
-q, --quiet quiet mode
-v, --verbose verbose mode
-s, --dry-run simulation mode
-R, --recursive recurse into subdirectories
-e, --escape expand escapes in old_string and new_string
-p, --prompt prompt before modifying each file
-f, --force ignore errors when trying to preserve permissions
-d, --keep-times keep the modification times on modified files
-t, --use-tmpdir use $TMPDIR for storing temporary files
-a, --all do not ignore files and directories starting with .
Example run "rpl" search and replace in all folders (-R) and files in my Documents folder ignoring case in simulation mode.

Code: Select all

rpl -s -i -p -R "PHD21" "phd21"  ~/Documents/*.*
Results using "-s" for simulation
user69@neon:~$ rpl -s -i -p -R "PHD21" "phd21" ~/Documents/*.*
Simulating replacement of "PHD21" with "phd21" (ignoring case) (partial words matched)
The files listed below would be modified in a replace operation.
/home/user69/Documents/addressbook_06182017.ldif
/home/user69/Documents/Gmail - Welcome to the SHOUTcast Streaming Service.html
/home/user69/Documents/Ring_ID.txt
/home/user69/Documents/SHOUTcast - Radiomanager.html

A Total of 39 matches found in 89 files searched.
None replaced (simulation mode).

Hope this helps ...
Phd21: Mint 20 and 19.2 Cinnamon & xKDE (Mint Xfce + Kubuntu KDE) & KDE Neon 64-bit (new based on Ubuntu 20.04) Awesome OS's, Dell Inspiron I5 7000 (7573) 2 in 1 touch screen, Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram, Intel 4 Graphics.
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: Find and replace

Post by Klaas Vaak »

@donalduck: many thanks for that. The multitude of / and \ seems quite tricky, enough to get one's knickers in a twist.
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: Find and replace

Post by Klaas Vaak »

Hi phd21, that seems to be a simple, straightforward solution. Nevertheless, you simulation just replaces 1 word with another.

How would I replace say
media/veracrypt1/Geopolitics/images/

with say
media/veracrypt1/Geopolitics/

BTW, to just find a word or words in a file contents I use Catfish that is included in the Mint package.
User avatar
phd21
Level 19
Level 19
Posts: 9673
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Find and replace

Post by phd21 »

Hi "Klaas Vaak",

"Catfish" is another great app for finding files.

With "rpl", I created a "/test" folder underneath my "Documents" folder and created two text files and put in your text multiple times in each text file to test this and this command worked quickly, but I had to be specific with the directory folder.

Code: Select all

rpl -i -R "media/veracrypt1/Geopolitics/images/" "media/veracrypt1/Geopolitics/"  ~/Documents/test/*.*

Hope this helps ...
Phd21: Mint 20 and 19.2 Cinnamon & xKDE (Mint Xfce + Kubuntu KDE) & KDE Neon 64-bit (new based on Ubuntu 20.04) Awesome OS's, Dell Inspiron I5 7000 (7573) 2 in 1 touch screen, Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram, Intel 4 Graphics.
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: Find and replace

Post by Klaas Vaak »

@phd21: thanks for that. RPL sure looks like a simple, efficient tool.
What I meant with that string is a path to a sub-sub-sub folder called "images".
Does RPL allow changing folder names too? In this case the part of the path to the "images" folder is shortened. Or is RPL purely for text replacement only? It is not entirely clear to me from the Source Forge page.
User avatar
phd21
Level 19
Level 19
Posts: 9673
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Find and replace

Post by phd21 »

Hi "Klaas Vaak",

You are welcome...

1.) "RPL" - I never knew of or used "rpl" until your post yesterday, but it does seem simple enough.

I don't think "rpl" can change directory folder paths. It appears to be a good text search and replace within file(s) application only.

Some of the console terminal commands could change the folder and or file names that match certain criteria for that purpose.

I guess that I am not sure what you are trying to do? At first it seemed like you wanted to replace text within a file or files, and now you want to change folder locations and or their file names as well, or do you want to do both? Doing both might require running two different applications and or console terminal commands, or one more complicated console terminal command.

Searching for and changing (replacing) directory folder names and or file names could be risky if you are not very careful. If you have the option of doing a "dry-run" simulation with any application and or console terminal command for these types of tasks, I would recommend doing that before actually changing anything to make sure it does what you want. Backing up first is always a good idea as well or taking a snapshot.

2.) "FAR" Find and Replace - seems to work well as long as you have Java installed. Simply download their self-contained ready to run archive file and extract it, then from the extracted folder "far", run their script "./far.sh" or "bash far.sh". I have not yet tested all that it can do, but it was easy enough to search for text within files and replace the text with something else.

Hope this helps ...
.
Far - Find and Replace Java application
Far - Find and Replace Java application
Phd21: Mint 20 and 19.2 Cinnamon & xKDE (Mint Xfce + Kubuntu KDE) & KDE Neon 64-bit (new based on Ubuntu 20.04) Awesome OS's, Dell Inspiron I5 7000 (7573) 2 in 1 touch screen, Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram, Intel 4 Graphics.
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: Find and replace

Post by Klaas Vaak »

@phd21: thanks for your detailed reply and caveats, once again :D
Yes, I started off with a need for text replacement within files, but that evolved into file name and folder name changes, although the latter is more of an academic interest at this stage.

You say you never knew of or used "rpl" until my post yesterday, but I got the idea of RPL from your 1st reply to me in this thread. Huh? Is this an example of a circular argument, or a misunderstanding? Whatever, it does not matter.

The reason I enquire about command line possibilities is because in Linux it is a lot more common to use those, plus it could avoid installing yet another app. Then again, when I see some of the syntax involved for F&R it seems to geeky for me. After all, my motto is: I don't want to have to be a mechanic to be able to drive the car.

I downloaded FAR 4 days ago and have been able to do an F&R exercise with it. I like the GUI. Today, following your earlier mail, I also tried DocFetcher, and even though the possibilities are impressive, it is too "fancy" for my needs, I prefer FAR.

1 last thing you might want to guide me on is a script to run FAR.

If I do this:

Code: Select all

#!/bin/bash
./far.sh
Is that OK? If not, what do I need to amend?
Where should I place the script?
Can I place a shortcut/symlink on the desktop to activate it with a click?

P.S.: I like your signature, and checked out the KDE link. I like XFCE, though that might be because of habit.
User avatar
phd21
Level 19
Level 19
Posts: 9673
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Find and replace

Post by phd21 »

Hi "Klaas Vaak",

You are welcome...again...
Klaas Vaak wrote:The reason I enquire about command line possibilities is because in Linux it is a lot more common to use those, plus it could avoid installing yet another app. Then again, when I see some of the syntax involved for F&R it seems to geeky for me. After all, my motto is: I don't want to have to be a mechanic to be able to drive the car.
Contrary to popular belief, most Linux Mint users do not have to use or learn the console terminal commands although it can certainly help. There are on occasion some maintenance and or alternate software application installation commands that people can use which can usually be copied and pasted from a post or reply.
Klaas Vaak wrote:I downloaded FAR 4 days ago and have been able to do an F&R exercise with it. I like the GUI. Today, following your earlier mail, I also tried DocFetcher, and even though the possibilities are impressive, it is too "fancy" for my needs, I prefer FAR.

1 last thing you might want to guide me on is a script to run FAR. Is that OK? If not, what do I need to amend? Where should I place the script? Can I place a shortcut/symlink on the desktop to activate it with a click?
I like the various file finder apps, but I am surprised they don't offer options for search and replace?

I like "FAR" over the console terminal options and it has worked well for me in my tests. There is even a "rename" tab that might work for your other task of renaming files and or folders, but I have not yet had a chance to test that.

You do not need to create another script to run the "far.sh" script. I usually copy or move software applications and their folder(s) like FAR from my Downloads folder where I download everything to underneath my "applications" or "apps" folder underneath my Home folder before I create desktop shortcut launchers or menu items.

You should be able to right-click your desktop and select create new desktop launcher and in the command box put the path location to the "far.sh" or if there is a file browse button to the right of the command box you can use that, then put in "./" or "bash " (there is a space after bash) in front of that. You can also create a menu item the same way; in Cinnamon, after creating a desktop shortcut launcher, it will ask you if you want it added to the menu, just say yes.

Example on my system - yours would be different and use your actual username

Code: Select all

bash /home/user69/apps/far/far.sh
or "~" tilda = alternative to using "/home/YourUserName/"

Code: Select all

bash ~/apps/far/far.sh
or

Code: Select all

./home/user69/apps/far/far.sh
or
Even this may work just using the filename without using "./" or bash ".

Code: Select all

/home/user69/apps/far/far.sh
Klaas Vaak wrote:P.S.: I like your signature, and checked out the KDE link. I like XFCE, though that might be because of habit.
You can easily install KDE Kubuntu desktop into Xfce (or Mate) and create what I call Linux Mint "xKDE" (Xfce) or "mKDE" (Mate) which works really well if you want all the cool KDE stuff.

Hope this helps ...
Phd21: Mint 20 and 19.2 Cinnamon & xKDE (Mint Xfce + Kubuntu KDE) & KDE Neon 64-bit (new based on Ubuntu 20.04) Awesome OS's, Dell Inspiron I5 7000 (7573) 2 in 1 touch screen, Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram, Intel 4 Graphics.
Klaas Vaak
Level 4
Level 4
Posts: 214
Joined: Fri Feb 16, 2018 6:18 am
Location: GMT+2

Re: Find and replace

Post by Klaas Vaak »

Thanks for your help, phd21, much appreciated.
I have just 1 small correction to make to your instruction, if I may. Maybe it is related to a difference in DE - your Cinnamon vs my XFCE - but this the way it is on XFCE:

in the Create Launcher dialogue window, in the Command box you put ./far.sh
and in the Working Directory box you put the path to the far.sh file.

I figured that out for myself - thank god one does not have to be an Einstein for that.
Once again, thanks for your help.
User avatar
phd21
Level 19
Level 19
Posts: 9673
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Find and replace

Post by phd21 »

Hi "Klaas Vaak",

You are welcome...

I must have missed where you said you were using Xfce.

Every wonderful edition of Linux Mint (Cinnamon, Mate, Xfce and KDE) have some differences in how they work and how they handle "scripts" (something.sh) which is why I try to show various options. Some editions do not have the option of putting in a working directory folder when creating desktop or menu shortcut launchers and sometimes (most of the time) you do not need to. Some editions including my KDE do not allow, or will not work correctly with, "./" in the command box when creating desktop or menu shortcut launchers which is why I use "bash AppStartupCommand".

FYI: With most Java applications, instead of using a script, you can also run their main ".jar" file after you have given yourself permission to run it using a similar command like below, but that did not work on my system with this particular application. And, if they do provide a script (.sh) to start their application, then using their script is usually the preferred startup option.

Code: Select all

java -jar ~/far/far.jar
.
Phd21: Mint 20 and 19.2 Cinnamon & xKDE (Mint Xfce + Kubuntu KDE) & KDE Neon 64-bit (new based on Ubuntu 20.04) Awesome OS's, Dell Inspiron I5 7000 (7573) 2 in 1 touch screen, Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram, Intel 4 Graphics.
Post Reply

Return to “Software & Applications”