LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2019, 12:21 PM   #1
halfpower
Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 241

Rep: Reputation: 31
Can sed replace multiple occurrences in a single line?


I have the following string:
Code:
ABCABC
I want to output
Code:
A B C A B C
I've tried doing it this way:
Code:
echo "ABCABC" | sed -E "s:(A)(B)(C)(.*):\1 \2 \3 \4:g"
However, this produces
Code:
A B C ABC
In my problem, the substring ABC can occur N times. Is sed capable of producing the desired output?
 
Old 09-14-2019, 12:22 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
How about this?

Code:
sed -e 's:.:& :g'
 
Old 09-14-2019, 02:09 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
another way

Code:
foo="ABCABC"
sed -E "s:([ABC]{1}):\1 :g" <<<$foo
A B C A B C 
sed -E "s:([AB]{1}):\1 :g"  <<<$foo
A B CA B C
sed -E "s:([AC]{1}):\1 :g"  <<<$foo
A BC A BC
 
Old 09-14-2019, 04:42 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
ABCABC
DEFGHIJK
... this sed ...
Code:
sed 's/./\0 /g' $InFile >$OutFile
... produced this OutFile ...
Code:
A B C A B C 
D E F G H I J K
Daniel B. Martin

.
 
Old 09-14-2019, 06:05 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Adding blank char to the end of line always causes subsequent confusion - especially when using an anchor with regex.
 
1 members found this post helpful.
Old 09-14-2019, 07:15 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
true

Code:
foo="ABCABC"
sed -E "s:([ABC]):\1 :g" <<<$foo | cat -A -
A B C A B C $
sed -E "s:([ABC]):\1 :g;s: $::" <<<$foo | cat -A -
A B C A B C$
I realised I didn't need {1}
 
Old 09-14-2019, 08:13 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204
As was shown in post#2 you don't need to capture all in a (group) - the sed-special & refers to all that was matched by the search.
Code:
sed 's:.:& :g; s: $::'
\0 might be a GNU-sed specific synonym for the & in a standard-sed.
 
Old 09-15-2019, 09:06 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by halfpower View Post
I've tried doing it this way:
Code:
echo "ABCABC" | sed -E "s:(A)(B)(C)(.*):\1 \2 \3 \4:g"
However, this produces
Code:
A B C ABC
Too much matching!
Code:
echo "ABCABC" | sed -E "s:(A)(B)(C):\1 \2 \3 :g"
Better?
 
  


Reply

Tags
regex, sed



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
[SOLVED] Need sed help - how to delete all but first two occurrences of a regexp per line kmkocot Linux - Newbie 2 08-25-2015 03:42 AM
[SOLVED] sed - replace alternate occurrences (on different lines) gazzatav Programming 5 04-16-2011 11:46 AM
[SOLVED] sed remove all occurrences in a string hattori.hanzo Linux - Newbie 5 11-22-2010 04:46 AM
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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