match space or newline in sed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I have read and search, but I don't understand what's wrong with it, I want to match: a space, the string 00011, and either a space or a new line.



sed 's:(s)(00011)([sn]):1$033:g'


EDIT: the data looks like this:



ADD 00000 00001 00011
LSH 00011 00100 01111
ADD 00011 10100 00010
JSR 00011101000111010101100010


and $03 is just a string to replace the 00011



I want to end up with something like this:



ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


Thanks










share|improve this question



















  • 1




    What does your input data look like? Note that since sed reads newline-delimited data, it will never see the newlines themselves.
    – Kusalananda
    50 mins ago










  • What does that $03 stand for? It won't be expanded due to the single quotes, and if it were, it would expand to e.g. bash3 (given your shell is bash).
    – RudiC
    44 mins ago










  • thanks, question edited.
    – onlycparra
    40 mins ago














up vote
3
down vote

favorite












I have read and search, but I don't understand what's wrong with it, I want to match: a space, the string 00011, and either a space or a new line.



sed 's:(s)(00011)([sn]):1$033:g'


EDIT: the data looks like this:



ADD 00000 00001 00011
LSH 00011 00100 01111
ADD 00011 10100 00010
JSR 00011101000111010101100010


and $03 is just a string to replace the 00011



I want to end up with something like this:



ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


Thanks










share|improve this question



















  • 1




    What does your input data look like? Note that since sed reads newline-delimited data, it will never see the newlines themselves.
    – Kusalananda
    50 mins ago










  • What does that $03 stand for? It won't be expanded due to the single quotes, and if it were, it would expand to e.g. bash3 (given your shell is bash).
    – RudiC
    44 mins ago










  • thanks, question edited.
    – onlycparra
    40 mins ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have read and search, but I don't understand what's wrong with it, I want to match: a space, the string 00011, and either a space or a new line.



sed 's:(s)(00011)([sn]):1$033:g'


EDIT: the data looks like this:



ADD 00000 00001 00011
LSH 00011 00100 01111
ADD 00011 10100 00010
JSR 00011101000111010101100010


and $03 is just a string to replace the 00011



I want to end up with something like this:



ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


Thanks










share|improve this question















I have read and search, but I don't understand what's wrong with it, I want to match: a space, the string 00011, and either a space or a new line.



sed 's:(s)(00011)([sn]):1$033:g'


EDIT: the data looks like this:



ADD 00000 00001 00011
LSH 00011 00100 01111
ADD 00011 10100 00010
JSR 00011101000111010101100010


and $03 is just a string to replace the 00011



I want to end up with something like this:



ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


Thanks







sed regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 30 mins ago

























asked 1 hour ago









onlycparra

466




466







  • 1




    What does your input data look like? Note that since sed reads newline-delimited data, it will never see the newlines themselves.
    – Kusalananda
    50 mins ago










  • What does that $03 stand for? It won't be expanded due to the single quotes, and if it were, it would expand to e.g. bash3 (given your shell is bash).
    – RudiC
    44 mins ago










  • thanks, question edited.
    – onlycparra
    40 mins ago












  • 1




    What does your input data look like? Note that since sed reads newline-delimited data, it will never see the newlines themselves.
    – Kusalananda
    50 mins ago










  • What does that $03 stand for? It won't be expanded due to the single quotes, and if it were, it would expand to e.g. bash3 (given your shell is bash).
    – RudiC
    44 mins ago










  • thanks, question edited.
    – onlycparra
    40 mins ago







1




1




What does your input data look like? Note that since sed reads newline-delimited data, it will never see the newlines themselves.
– Kusalananda
50 mins ago




What does your input data look like? Note that since sed reads newline-delimited data, it will never see the newlines themselves.
– Kusalananda
50 mins ago












What does that $03 stand for? It won't be expanded due to the single quotes, and if it were, it would expand to e.g. bash3 (given your shell is bash).
– RudiC
44 mins ago




What does that $03 stand for? It won't be expanded due to the single quotes, and if it were, it would expand to e.g. bash3 (given your shell is bash).
– RudiC
44 mins ago












thanks, question edited.
– onlycparra
40 mins ago




thanks, question edited.
– onlycparra
40 mins ago










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










sed works on a line at a time and it will strip the newlines when processing each line.



So, in order to do what you want, you should match the end of line anchor ($) rather than a literal newline character.



This should work:



sed 's:(s)(00011)(s|$):1$033:g'





share|improve this answer
















  • 1




    I already tried that, but I missed the backslash before the pipe, thanks, it works perfectly.
    – onlycparra
    18 mins ago






  • 1




    This would fail for data such as 00011 00011.
    – Kusalananda
    7 mins ago

















up vote
2
down vote













$ sed 's/<00011>/$03/g' file
ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


The < and > matches the zero-width word boundaries at the start and end of a word, respectively. BSD sed would also recognise [[:<:]] and [[:>:]], and GNU sed also understands b as a word boundary.



sed will never see the newlines in the input data. Also, s is specific to GNU sed. To match a space character in standard sed just use a literal space (to match a space-or-tab, use [[:blank:]]).






share|improve this answer


















  • 1




    very complete and informative, thanks
    – onlycparra
    15 mins ago

















up vote
0
down vote













This should to the job (a little bit cleaner to read):



sed -r 's/(s)00011(s|$)/1$32/'


or even:



sed -r 's/( )00011( |$)/1$32/'


The output:



ADD 00000 00001 $3
LSH $3 00100 01111
ADD $3 10100 00010
JSR 00011101000111010101100010





share|improve this answer




















  • This, too, would fail on the data 00011 00011.
    – Kusalananda
    4 mins ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471522%2fmatch-space-or-newline-in-sed%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










sed works on a line at a time and it will strip the newlines when processing each line.



So, in order to do what you want, you should match the end of line anchor ($) rather than a literal newline character.



This should work:



sed 's:(s)(00011)(s|$):1$033:g'





share|improve this answer
















  • 1




    I already tried that, but I missed the backslash before the pipe, thanks, it works perfectly.
    – onlycparra
    18 mins ago






  • 1




    This would fail for data such as 00011 00011.
    – Kusalananda
    7 mins ago














up vote
2
down vote



accepted










sed works on a line at a time and it will strip the newlines when processing each line.



So, in order to do what you want, you should match the end of line anchor ($) rather than a literal newline character.



This should work:



sed 's:(s)(00011)(s|$):1$033:g'





share|improve this answer
















  • 1




    I already tried that, but I missed the backslash before the pipe, thanks, it works perfectly.
    – onlycparra
    18 mins ago






  • 1




    This would fail for data such as 00011 00011.
    – Kusalananda
    7 mins ago












up vote
2
down vote



accepted







up vote
2
down vote



accepted






sed works on a line at a time and it will strip the newlines when processing each line.



So, in order to do what you want, you should match the end of line anchor ($) rather than a literal newline character.



This should work:



sed 's:(s)(00011)(s|$):1$033:g'





share|improve this answer












sed works on a line at a time and it will strip the newlines when processing each line.



So, in order to do what you want, you should match the end of line anchor ($) rather than a literal newline character.



This should work:



sed 's:(s)(00011)(s|$):1$033:g'






share|improve this answer












share|improve this answer



share|improve this answer










answered 32 mins ago









Filipe Brandenburger

3,787622




3,787622







  • 1




    I already tried that, but I missed the backslash before the pipe, thanks, it works perfectly.
    – onlycparra
    18 mins ago






  • 1




    This would fail for data such as 00011 00011.
    – Kusalananda
    7 mins ago












  • 1




    I already tried that, but I missed the backslash before the pipe, thanks, it works perfectly.
    – onlycparra
    18 mins ago






  • 1




    This would fail for data such as 00011 00011.
    – Kusalananda
    7 mins ago







1




1




I already tried that, but I missed the backslash before the pipe, thanks, it works perfectly.
– onlycparra
18 mins ago




I already tried that, but I missed the backslash before the pipe, thanks, it works perfectly.
– onlycparra
18 mins ago




1




1




This would fail for data such as 00011 00011.
– Kusalananda
7 mins ago




This would fail for data such as 00011 00011.
– Kusalananda
7 mins ago












up vote
2
down vote













$ sed 's/<00011>/$03/g' file
ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


The < and > matches the zero-width word boundaries at the start and end of a word, respectively. BSD sed would also recognise [[:<:]] and [[:>:]], and GNU sed also understands b as a word boundary.



sed will never see the newlines in the input data. Also, s is specific to GNU sed. To match a space character in standard sed just use a literal space (to match a space-or-tab, use [[:blank:]]).






share|improve this answer


















  • 1




    very complete and informative, thanks
    – onlycparra
    15 mins ago














up vote
2
down vote













$ sed 's/<00011>/$03/g' file
ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


The < and > matches the zero-width word boundaries at the start and end of a word, respectively. BSD sed would also recognise [[:<:]] and [[:>:]], and GNU sed also understands b as a word boundary.



sed will never see the newlines in the input data. Also, s is specific to GNU sed. To match a space character in standard sed just use a literal space (to match a space-or-tab, use [[:blank:]]).






share|improve this answer


















  • 1




    very complete and informative, thanks
    – onlycparra
    15 mins ago












up vote
2
down vote










up vote
2
down vote









$ sed 's/<00011>/$03/g' file
ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


The < and > matches the zero-width word boundaries at the start and end of a word, respectively. BSD sed would also recognise [[:<:]] and [[:>:]], and GNU sed also understands b as a word boundary.



sed will never see the newlines in the input data. Also, s is specific to GNU sed. To match a space character in standard sed just use a literal space (to match a space-or-tab, use [[:blank:]]).






share|improve this answer














$ sed 's/<00011>/$03/g' file
ADD 00000 00001 $03
LSH $03 00100 01111
ADD $03 10100 00010
JSR 00011101000111010101100010


The < and > matches the zero-width word boundaries at the start and end of a word, respectively. BSD sed would also recognise [[:<:]] and [[:>:]], and GNU sed also understands b as a word boundary.



sed will never see the newlines in the input data. Also, s is specific to GNU sed. To match a space character in standard sed just use a literal space (to match a space-or-tab, use [[:blank:]]).







share|improve this answer














share|improve this answer



share|improve this answer








edited 19 mins ago

























answered 24 mins ago









Kusalananda

107k14209331




107k14209331







  • 1




    very complete and informative, thanks
    – onlycparra
    15 mins ago












  • 1




    very complete and informative, thanks
    – onlycparra
    15 mins ago







1




1




very complete and informative, thanks
– onlycparra
15 mins ago




very complete and informative, thanks
– onlycparra
15 mins ago










up vote
0
down vote













This should to the job (a little bit cleaner to read):



sed -r 's/(s)00011(s|$)/1$32/'


or even:



sed -r 's/( )00011( |$)/1$32/'


The output:



ADD 00000 00001 $3
LSH $3 00100 01111
ADD $3 10100 00010
JSR 00011101000111010101100010





share|improve this answer




















  • This, too, would fail on the data 00011 00011.
    – Kusalananda
    4 mins ago














up vote
0
down vote













This should to the job (a little bit cleaner to read):



sed -r 's/(s)00011(s|$)/1$32/'


or even:



sed -r 's/( )00011( |$)/1$32/'


The output:



ADD 00000 00001 $3
LSH $3 00100 01111
ADD $3 10100 00010
JSR 00011101000111010101100010





share|improve this answer




















  • This, too, would fail on the data 00011 00011.
    – Kusalananda
    4 mins ago












up vote
0
down vote










up vote
0
down vote









This should to the job (a little bit cleaner to read):



sed -r 's/(s)00011(s|$)/1$32/'


or even:



sed -r 's/( )00011( |$)/1$32/'


The output:



ADD 00000 00001 $3
LSH $3 00100 01111
ADD $3 10100 00010
JSR 00011101000111010101100010





share|improve this answer












This should to the job (a little bit cleaner to read):



sed -r 's/(s)00011(s|$)/1$32/'


or even:



sed -r 's/( )00011( |$)/1$32/'


The output:



ADD 00000 00001 $3
LSH $3 00100 01111
ADD $3 10100 00010
JSR 00011101000111010101100010






share|improve this answer












share|improve this answer



share|improve this answer










answered 29 mins ago









Ravexina

990719




990719











  • This, too, would fail on the data 00011 00011.
    – Kusalananda
    4 mins ago
















  • This, too, would fail on the data 00011 00011.
    – Kusalananda
    4 mins ago















This, too, would fail on the data 00011 00011.
– Kusalananda
4 mins ago




This, too, would fail on the data 00011 00011.
– Kusalananda
4 mins ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471522%2fmatch-space-or-newline-in-sed%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery