0

I am trying to setup vagrant box using shell script where in I am storing key and cert files. Normal format of any cert or key file is as below

-----BEGIN CERTIFICATE-----
MIIDzzCCAregAwIBAgIJAP384f0KeRndMA0GCSqGSIb3DQEBCwUAMH4xCzAJBgNV
....
0+ZQ+Yxjzs69VHdPJbzu3cxjNQ==
-----END CERTIFICATE-----

-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC6qoE3hNniDqD7
....
wr5wa+bGBgNzIBLS5j9F9Kul
-----END PRIVATE KEY-----

However when I check on vagrant box it looks like below.

-----BEGIN CERTIFICATE----- MIIDzzCCAregAwIB ..... 69VHdPJbzu3cxjNQ== -----END CERTIFICATE-----

-----BEGIN PRIVATE KEY----- MIIEvgIBADANBgkqhki  .... OB7jnwaqq wr5wa+bGBgNzIBLS5j9F9Kul -----END PRIVATE KEY-----

(basically, everything in one row)

Due to which Apache2 has errors in the log. Now I want to convert these keys to their proper format. I did try below

cat server.crt | tr " " "\n" 
cat server.crt | tr "\ /" " " 

but it didn't help.

    -----BEGIN
CERTIFICATE-----
MIIDzzCCAregAwIBAgIJAP384f0KeRndMA0GCSqGSIb3DQEBCwUAMH4xCzAJBgNV

0+ZQ+Yxjzs69VHdPJbzu3cxjNQ==
-----END
CERTIFICATE-----

This is what I am getting as a result of sed -i 's/ /\n/g' *.crt command. after END it goes to next line due to space character. Objective here is keep the first and last line as it is and format the content between them.

Shailesh Sutar
  • 1,517
  • 5
  • 23
  • 41

2 Answers2

0

You want sed -i 's/ /\n/g' *.crt (assuming you have GNU sed)

But have you addressed the root cause? How did the files get corrupted?

glenn jackman
  • 4,630
  • 1
  • 17
  • 20
  • I am sending the whole file as variable in shell script which is causing this change in file. – Shailesh Sutar Sep 07 '17 at 15:52
  • -----BEGIN CERTIFICATE----- MIIDzzCCAregAwIBAgIJAP384f0KeRndMA0GCSqGSIb3DQEBCwUAMH4xCzAJBgNV ----------------------------------------- 0+ZQ+Yxjzs69VHdPJbzu3cxjNQ== -----END CERTIFICATE----- This is what I am getting as a result of this command. after END it goes to next line due to space character. – Shailesh Sutar Sep 07 '17 at 16:38
  • Do you quote your `"$variable"` ? – glenn jackman Sep 07 '17 at 16:46
  • You should read this: https://unix.stackexchange.com/q/171346/4667 – glenn jackman Sep 07 '17 at 16:55
0

You should use tr '\ ' '\n'

Example:

[root@localhost ~]# uname -a
Linux localhost 2.6.32-504.16.2.el6.x86_64 #1 SMP Tue Mar 10 17:01:00 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# uname -a | tr '\ ' '\n'
Linux
localhost
2.6.32-504.16.2.el6.x86_64
#1
SMP
Tue
Mar
10
17:01:00
EDT
2015
x86_64
x86_64
x86_64
GNU/Linux
Godvil
  • 21
  • 1