LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 03-26-2019, 06:46 PM   #1
dezix
Member
 
Registered: Sep 2017
Location: Frog's Land
Distribution: Debian
Posts: 98

Rep: Reputation: Disabled
bash : (find + grep + sed) to edit only some of matching occurrences


Hi!

In a <path> (directory) containing some <file>.txt
them self containing lines like:

Code:
<chain1><something else>
If I use the following command:

Code:
find <path> -type f -name "*.txt" -exec sed -i 's/<chain1>/<chain2>/g' '{}' \;
every:

Code:
<chain1><something else>
will be changed to:

Code:
<chain2><something else>

But what I really want, is only to change :

Code:
<chain1><something else containing <chain3> >
in:

Code:
<chain2><something else containing <chain3> >

Is there some way to insert a
Code:
grep <chain3>
in the general command above to get this result?


Thanks for help!
 
Old 03-26-2019, 09:16 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by dezix View Post
But what I really want, is only to change :
Code:
<chain1><something else containing <chain3> >
in:
Code:
<chain2><something else containing <chain3> >
sed has grep built-in, so to speak.

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.
 
Old 03-26-2019, 09:47 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 03-26-2019 at 09:59 PM.
 
Old 03-27-2019, 04:59 AM   #4
dezix
Member
 
Registered: Sep 2017
Location: Frog's Land
Distribution: Debian
Posts: 98

Original Poster
Rep: Reputation: Disabled
@berndbausch

Your first "lazy" proposal works perfectly )

sed is a very usefull piece of code

I thank you!


@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 )
 
Old 03-27-2019, 07:03 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by dezix View Post
@berndbausch

Your first "lazy" proposal works perfectly )

sed is a very usefull piece of code

I thank you!


@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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
[SOLVED] sed remove all occurrences in a string hattori.hanzo Linux - Newbie 5 11-22-2010 04:46 AM
command grep issue: how to get occurrences of an pattern look like "cool.a_string" coolloo_djack Linux - General 4 03-13-2010 09:27 AM
[SOLVED] Need sed help: s/ command won't replace two occurrences of pattern on same line GrapefruiTgirl Programming 7 12-16-2009 02:08 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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