LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 10-21-2018, 02:46 PM   #16
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721

Quote:
Originally Posted by scasey View Post
(untested)
You'll need to separate the options, as in "sed -i -e", because any string after the -i will be tacked on as a suffix to the backup file name. Usually it's done like this

Code:
sed -iorig -e 's/.../.../g'

sed -iback -e 's/.../.../g'
 
1 members found this post helpful.
Old 10-21-2018, 02:52 PM   #17
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by Turbocapitalist View Post
You'll need to separate the options, as in "sed -i -e", because any string after the -i will be tacked on as a suffix to the backup file name. Usually it's done like this

Code:
sed -iorig -e 's/.../.../g'

sed -iback -e 's/.../.../g'
Oh! Good catch! I seldom use the -i option. Note that -ei should work (with no backup designator), but separating them is probably clearer in any case.
 
Old 10-22-2018, 02:34 AM   #18
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by scasey View Post
Oh! Good catch! I seldom use the -i option. Note that -ei should work (with no backup designator), but separating them is probably clearer in any case.
Or just sed -i as there is only one script
 
Old 10-22-2018, 08:07 AM   #19
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,715

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by Turbocapitalist View Post
Are you getting the backslash in before the \b? Without a backslash, it's just a b. Same for the > and <
Hi TC. Sorry but I'm confused.
 
Old 10-22-2018, 08:08 AM   #20
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,715

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by scasey View Post
Code:
find . -name *.txt -exec sed -ie 's/\bone\b/1/g' {} \;
(untested)

#3 gives you the answers, but we're expecting you to plug them in to what you know...also, the examples in #3 didn't include the -i, which is what causes the change to be written to the file instead of just being displayed.

It's difficult to teach regular expressions (regexp) a piece at a time. Search for something like "regexp syntax" or get a good book. The chapter on the subject in "UNIX in a Nutshell" is excellent, IMO. Certainly my copy is very dog-eared.
Hi Sean.

That created a new file with the extension '.txte' and didn't rename any word.
 
Old 10-22-2018, 08:08 AM   #21
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,715

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Post

Hi guys.

I will settle with Sean's answer https://www.linuxquestions.org/quest...9/#post5916393.
 
Old 10-22-2018, 08:28 AM   #22
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by linustalman View Post
Why don't you use the following?
Code:
find . -name *.txt -exec sed -i 's/\beleven\b/11/g' {} +

Last edited by l0f4r0; 10-22-2018 at 08:31 AM.
 
Old 10-22-2018, 08:35 AM   #23
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,715

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Post

Quote:
Originally Posted by l0f4r0 View Post
Why don't you use the following?
Code:
find . -name *.txt -exec sed -i 's/\beleven\b/11/g' {} +
Hi l0f4r0. That too worked fine except if you for example want to turn the word 'one' into '1' - it also replaces all words that contain 'one' into 1 (e.g. 'someone' changes to '1').
 
Old 10-22-2018, 08:35 AM   #24
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by linustalman View Post
That created a new file with the extension '.txte' and didn't rename any word.
That's because of the typo -ie (explained in #16).
Your file(s) *.txte are backup files so no change there.
Check your file(s) *.txt, they reflect changes.

Last edited by l0f4r0; 10-22-2018 at 08:43 AM.
 
Old 10-22-2018, 08:37 AM   #25
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,715

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by l0f4r0 View Post
That's because of the typo -ie (explained in #16).
Your file(s) *.txte are backup files so no change there.
Check your file(s) *.txt, they reflex changes.
Nothing changed in either.
 
Old 10-22-2018, 08:40 AM   #26
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by linustalman View Post
Hi l0f4r0. That too worked fine except if you for example want to turn the word 'one' into '1' - it also replaces all words that contain 'one' into 1 (e.g. 'someone' changes to '1').
Really?? It's weird because \b used in the sed search pattern are specified in order to avoid this behavior...
As already suggested in #3, what about:
Code:
find . -name *.txt -exec sed -i 's/\<eleven\>/11/g' {} +
?

Can you run:
Code:
sed --version
?

Last edited by l0f4r0; 10-22-2018 at 08:41 AM.
 
1 members found this post helpful.
Old 10-22-2018, 08:45 AM   #27
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by linustalman View Post
Nothing changed in either.
Strange...
Anyway, forget about -ie. Either write -i -e, or only -i
 
Old 10-22-2018, 08:46 AM   #28
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,715

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Post

Quote:
Originally Posted by l0f4r0 View Post
Really?? It's weird because \b used in the sed search pattern are specified in order to avoid this behavior...
As already suggested in #3, what about:
Code:
find . -name *.txt -exec sed -i 's/\<eleven\>/11/g' {} +
?

Can you run:
Code:
sed --version
?
Re:
Code:
find . -name *.txt -exec sed -i 's/\<eleven\>/11/g' {} +
Excellent. It left 'someone' alone and only changed 'one' to '1'.

I'm on sed (GNU sed) 4.4.

Last edited by linustalman; 10-22-2018 at 08:47 AM.
 
  


Reply

Tags
word replacement



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
[SOLVED] Search for a character in specific word in file and replace it in the word RM2015 Linux - Newbie 9 06-02-2015 07:38 AM
[SOLVED] Replace text lines identified by leading text in line within multiple files juergen852 Linux - Newbie 9 09-21-2014 04:54 PM
[SOLVED] Replace all instances of a word in a group of files? Fingers of Fudge Linux - Newbie 2 09-11-2013 05:29 PM
[SOLVED] Find/Replace shell script that replaces word with other word in text, filenames yanom Programming 8 09-12-2012 12:29 AM
sed 1st word to replace all instances of ( donnied Programming 5 08-21-2008 06:43 PM

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

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