0

We receive a file which is essentially an ssh token key. This upon inception has values, say

Foo\nBarFoo,\nFoo\nBarFoo

Now, I want to replace these with

Foo
BarFoo,
Foo
BarFoo

I have tried sed and tr commands by copying the entire key in a variable. One thing that seemed to work was : %s/\\n/\r/g, but this is not acceptable since I cannot open the vi editor.

I recently tried echo -e 'Foo\nBarFoo,\nFoo\nBarFoo, but want to be it more subtle.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • What is the problem using that pattern with sed? – Lennart - Slava Ukraini Jul 14 '19 at 17:43
  • [This stackoverflow post](https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed) answers your question. Search before you ask. – Mihir Luthra Jul 14 '19 at 17:54
  • What have you tried? What didn't it work? `I have tried sed and tr commands by copying the entire key in a variable` what have you tried exactly? Just give example command, example output, such small code snippets help a lot. Note that there is `ed` command, which exactly executes `vi`-like commands in batch mode. And, wouldn't just `echo ': %s/\\n/\r/g' | vi input_file` work? – KamilCuk Jul 14 '19 at 18:16
  • @Mihir that post is about substituting a newline with another character. This is about substituting the string `\n` with a newline. These are not duplicates. – KamilCuk Jul 14 '19 at 18:16
  • @KamilCuk, thanks for pointing that out, my mistake. – Mihir Luthra Jul 15 '19 at 06:28
  • Why not just `echo -e $( – David C. Rankin Jul 15 '19 at 06:52
  • Thanks David. But this is exactly what I am using now (echo -e 'pointed in the main body above') – pratik swami Jul 15 '19 at 15:11

1 Answers1

2

You are facing the problem because you are using OSX or BSD probably. With GNU sed, 's/\\n/\n/g’ should have worked.

For POSIX sed, use this,

echo "Foo\nBarFoo,\nFoo\nBarFoo" | sed 's/\\n/\
/g'
Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39