LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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: 212

Rep: Reputation: 30
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
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,160
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
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,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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,729

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
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: 18,127

Rep: Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923Reputation: 2923
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,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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: 1,193

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
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: 12,463
Blog Entries: 9

Rep: Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372
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


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 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 11:56 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration