[SOLVED] bash : (find + grep + sed) to edit only some of matching occurrences
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am lazy and assume that <chain1> always occurs before <chain3> in a line.
Code:
sed '/<chain3>/s/<chain1>/<chain2>/'
This performs the replacement in all lines that contain the string <chain3>. Strictly speaking, this is not quite what you ask for, but as long as my assumption is correct, it does the job.
OK, here is a more water-tight way to do it:
Code:
sed '/<chain1>.*<chain3>/s/<chain1>/<chain2>/'
Here, the replacement is made in lines that contain <chain1> followed by any string (including the empty string) followed by <chain3>.
Last edited by berndbausch; 03-26-2019 at 09:21 PM.
maybe in a script due to if else clause. if you're doing simple name changing.
Code:
#!/bin/bash
working_dir-/whatever/it/is
chain1=
chain2=
chain3=
while read s
do
if [[ "$s" =~ "$chain1" ]] ;
then
mv "$s" "${s/chain1/chain2}"
else
mv "$s" "${s/"$chain3"}"
fi
done <<<"$(find "$working_dir" -type f -name "*.txt")"
if you are doing text within a file then that water-tight one above, try that.
@BW-userx
Effectively I was wondering myself if this action require some logical/conditional statement to be achieved
But I still don't know how to do that (
I've tried a few around your example script,
but that doesn't work.
What I don't understand is why to use mv,
my intention is to modify the files content not there names.
@BW-userx
Effectively I was wondering myself if this action require some logical/conditional statement to be achieved
But I still don't know how to do that (
I've tried a few around your example script,
but that doesn't work.
What I don't understand is why to use mv,
my intention is to modify the files content not there names.
I thank you too for the trail
For me the subject is solved )
I wasn't fully understanding what you are trying to do at the time, as state for a simple name change of the files name. the sed command fixes text within a file. with sed you'd have to set it up to search both conditions if one is found it changes that, if the other is found then it changes that instead, without the use of if else.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.