0

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 =?

  • 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
4

You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement

sed '/^~*$/s/~/=/g'
  • 3
    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
  • 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's sed: it's not for everyone :) – don_crissti Jan 14 at 23:09
0

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 ~]#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.