Find all lines containing only the same character and replace with the same amount of another character
My goal is to convert a NEWS format (the output of appstream-util appdata-to-news
) to a markdown format (to use on GitHub/GitLab).
A good approximation for me would be to just go from this:
Version 0.5.1
~~~~~~~~~~~~~
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
to this
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
But my current solution with tr '~' '='
gives me this:
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a = tilde ====
* Second line
The regex to find all the lines containing only ~
should be this: ^~*$
But how can, with bash/sed/awk, replace with the same amount of =
?
text-processing awk sed
New contributor
add a comment |
My goal is to convert a NEWS format (the output of appstream-util appdata-to-news
) to a markdown format (to use on GitHub/GitLab).
A good approximation for me would be to just go from this:
Version 0.5.1
~~~~~~~~~~~~~
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
to this
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
But my current solution with tr '~' '='
gives me this:
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a = tilde ====
* Second line
The regex to find all the lines containing only ~
should be this: ^~*$
But how can, with bash/sed/awk, replace with the same amount of =
?
text-processing awk sed
New contributor
Is it only tilde-only lines you want to replace, or are there other characters?
– DopeGhoti
Jan 14 at 22:03
As far as I know is tilde only, beside the new line at the end.
– Roberto Leinardi
Jan 14 at 22:04
add a comment |
My goal is to convert a NEWS format (the output of appstream-util appdata-to-news
) to a markdown format (to use on GitHub/GitLab).
A good approximation for me would be to just go from this:
Version 0.5.1
~~~~~~~~~~~~~
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
to this
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
But my current solution with tr '~' '='
gives me this:
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a = tilde ====
* Second line
The regex to find all the lines containing only ~
should be this: ^~*$
But how can, with bash/sed/awk, replace with the same amount of =
?
text-processing awk sed
New contributor
My goal is to convert a NEWS format (the output of appstream-util appdata-to-news
) to a markdown format (to use on GitHub/GitLab).
A good approximation for me would be to just go from this:
Version 0.5.1
~~~~~~~~~~~~~
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
to this
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
But my current solution with tr '~' '='
gives me this:
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a = tilde ====
* Second line
The regex to find all the lines containing only ~
should be this: ^~*$
But how can, with bash/sed/awk, replace with the same amount of =
?
text-processing awk sed
text-processing awk sed
New contributor
New contributor
edited Jan 14 at 22:25
don_crissti
50.3k15134162
50.3k15134162
New contributor
asked Jan 14 at 21:59
Roberto LeinardiRoberto Leinardi
1033
1033
New contributor
New contributor
Is it only tilde-only lines you want to replace, or are there other characters?
– DopeGhoti
Jan 14 at 22:03
As far as I know is tilde only, beside the new line at the end.
– Roberto Leinardi
Jan 14 at 22:04
add a comment |
Is it only tilde-only lines you want to replace, or are there other characters?
– DopeGhoti
Jan 14 at 22:03
As far as I know is tilde only, beside the new line at the end.
– Roberto Leinardi
Jan 14 at 22:04
Is it only tilde-only lines you want to replace, or are there other characters?
– DopeGhoti
Jan 14 at 22:03
Is it only tilde-only lines you want to replace, or are there other characters?
– DopeGhoti
Jan 14 at 22:03
As far as I know is tilde only, beside the new line at the end.
– Roberto Leinardi
Jan 14 at 22:04
As far as I know is tilde only, beside the new line at the end.
– Roberto Leinardi
Jan 14 at 22:04
add a comment |
2 Answers
2
active
oldest
votes
You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement
sed '/^~*$/s/~/=/g'
It works! Thanks!
– Roberto Leinardi
Jan 14 at 22:05
3
orsed '/[^~]/!y/~/=/'
if you want to usesed
's builtintr
– don_crissti
Jan 14 at 22:28
@don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...
– roaima
Jan 14 at 22:59
1
Or,sed -E '/^(.)1*$/ s/./=/g'
which does not hardcode the tilde character.
– glenn jackman
Jan 14 at 23:06
It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it'ssed
: it's not for everyone :)
– don_crissti
Jan 14 at 23:09
add a comment |
I Tried with below sed command and it worked fine
Command: sed '/Version/{n;s/~/=/g}' filename
e
n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
[root@praveen_linux_example ~]#
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Roberto Leinardi is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494501%2ffind-all-lines-containing-only-the-same-character-and-replace-with-the-same-amou%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement
sed '/^~*$/s/~/=/g'
It works! Thanks!
– Roberto Leinardi
Jan 14 at 22:05
3
orsed '/[^~]/!y/~/=/'
if you want to usesed
's builtintr
– don_crissti
Jan 14 at 22:28
@don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...
– roaima
Jan 14 at 22:59
1
Or,sed -E '/^(.)1*$/ s/./=/g'
which does not hardcode the tilde character.
– glenn jackman
Jan 14 at 23:06
It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it'ssed
: it's not for everyone :)
– don_crissti
Jan 14 at 23:09
add a comment |
You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement
sed '/^~*$/s/~/=/g'
It works! Thanks!
– Roberto Leinardi
Jan 14 at 22:05
3
orsed '/[^~]/!y/~/=/'
if you want to usesed
's builtintr
– don_crissti
Jan 14 at 22:28
@don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...
– roaima
Jan 14 at 22:59
1
Or,sed -E '/^(.)1*$/ s/./=/g'
which does not hardcode the tilde character.
– glenn jackman
Jan 14 at 23:06
It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it'ssed
: it's not for everyone :)
– don_crissti
Jan 14 at 23:09
add a comment |
You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement
sed '/^~*$/s/~/=/g'
You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement
sed '/^~*$/s/~/=/g'
answered Jan 14 at 22:03
roaimaroaima
43.5k553116
43.5k553116
It works! Thanks!
– Roberto Leinardi
Jan 14 at 22:05
3
orsed '/[^~]/!y/~/=/'
if you want to usesed
's builtintr
– don_crissti
Jan 14 at 22:28
@don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...
– roaima
Jan 14 at 22:59
1
Or,sed -E '/^(.)1*$/ s/./=/g'
which does not hardcode the tilde character.
– glenn jackman
Jan 14 at 23:06
It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it'ssed
: it's not for everyone :)
– don_crissti
Jan 14 at 23:09
add a comment |
It works! Thanks!
– Roberto Leinardi
Jan 14 at 22:05
3
orsed '/[^~]/!y/~/=/'
if you want to usesed
's builtintr
– don_crissti
Jan 14 at 22:28
@don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...
– roaima
Jan 14 at 22:59
1
Or,sed -E '/^(.)1*$/ s/./=/g'
which does not hardcode the tilde character.
– glenn jackman
Jan 14 at 23:06
It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it'ssed
: it's not for everyone :)
– don_crissti
Jan 14 at 23:09
It works! Thanks!
– Roberto Leinardi
Jan 14 at 22:05
It works! Thanks!
– Roberto Leinardi
Jan 14 at 22:05
3
3
or
sed '/[^~]/!y/~/=/'
if you want to use sed
's builtin tr
– don_crissti
Jan 14 at 22:28
or
sed '/[^~]/!y/~/=/'
if you want to use sed
's builtin tr
– don_crissti
Jan 14 at 22:28
@don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...
– roaima
Jan 14 at 22:59
@don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...
– roaima
Jan 14 at 22:59
1
1
Or,
sed -E '/^(.)1*$/ s/./=/g'
which does not hardcode the tilde character.– glenn jackman
Jan 14 at 23:06
Or,
sed -E '/^(.)1*$/ s/./=/g'
which does not hardcode the tilde character.– glenn jackman
Jan 14 at 23:06
It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's
sed
: it's not for everyone :)– don_crissti
Jan 14 at 23:09
It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's
sed
: it's not for everyone :)– don_crissti
Jan 14 at 23:09
add a comment |
I Tried with below sed command and it worked fine
Command: sed '/Version/{n;s/~/=/g}' filename
e
n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
[root@praveen_linux_example ~]#
add a comment |
I Tried with below sed command and it worked fine
Command: sed '/Version/{n;s/~/=/g}' filename
e
n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
[root@praveen_linux_example ~]#
add a comment |
I Tried with below sed command and it worked fine
Command: sed '/Version/{n;s/~/=/g}' filename
e
n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
[root@praveen_linux_example ~]#
I Tried with below sed command and it worked fine
Command: sed '/Version/{n;s/~/=/g}' filename
e
n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
Version 0.5.1
=============
Released: 2019-01-03
* This is a test with a ~ tilde ~~~~
* Second line
[root@praveen_linux_example ~]#
answered Jan 16 at 18:10
Praveen Kumar BSPraveen Kumar BS
1,346138
1,346138
add a comment |
add a comment |
Roberto Leinardi is a new contributor. Be nice, and check out our Code of Conduct.
Roberto Leinardi is a new contributor. Be nice, and check out our Code of Conduct.
Roberto Leinardi is a new contributor. Be nice, and check out our Code of Conduct.
Roberto Leinardi is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494501%2ffind-all-lines-containing-only-the-same-character-and-replace-with-the-same-amou%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is it only tilde-only lines you want to replace, or are there other characters?
– DopeGhoti
Jan 14 at 22:03
As far as I know is tilde only, beside the new line at the end.
– Roberto Leinardi
Jan 14 at 22:04