0

I am using a bash shell and trying to process text file to replace line breaks with a single line string with "\n"s. Example:

Source file:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LOOOONG-STRING
    server: https://1.2.3.4:8443
  name: cluster-name
contexts:
- context:
    cluster: cluster-name
    user: cluster-admin
  name: cluster-admin@cluster-name
current-context: cluster-admin@cluster-name
kind: Config
preferences: {}
users:
- name: cluster-admin
  user:
    client-certificate-data: LOOOONG-STRING
    client-key-data: LOOOONG-STRING

Desired Output

apiVersion: v1\nclusters:\n- cluster:\n    certificate-authority-data: LOOOONG-STRING\n    server: https://1.2.3.4:8443\n  name: cluster-name\ncontexts:\n- context:\n    cluster: cluster-name\n    user: cluster-admin\n  name: cluster-admin@cluster-name\ncurrent-context: cluster-admin@cluster-name\nkind: Config\npreferences: {}\nusers:\n- name: cluster-admin\n  user:\n    client-certificate-data: LOOOONG-STRING\n    client-key-data: LOOOONG-STRING

I tried many tr and sed examples here and cannot get it done. thanks.

Honshu
  • 13
  • 5
  • Possible duplicate of [How can I replace a newline (\n) using sed?](https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed) – Baba Feb 06 '19 at 06:54
  • have a look at this: https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed – Tim Rutter Feb 06 '19 at 06:54
  • 2
    What is your expected output with the source file? – Allan Feb 06 '19 at 07:35
  • That you are using bash and the contents of your file are YAML are completely irrelevant to your question. You just want to convert a multiline text file to a single line where the originals are seperate by `\n`. This is a question for the Unix and Linux StackExchange website. – Anthon Feb 06 '19 at 10:07

1 Answers1

0

Try this:

sed -e :a -e '$!N;s/\n/\\\n/;ta' file

With Gnu sed, you may need to escape new line only once:

sed -e :a -e '$!N;s/\n/\\n/;ta' file

All lines are joined and new line characters replaced with \\\n.

You can add the -i flag to edit the file in place or redirect output to a new file:

sed -e :a -e '$!N;s/\n/\\\n/;ta' file > newfile
SLePort
  • 15,211
  • 3
  • 34
  • 44
  • Thank you SlePort! I tried which worked only for half of the file. I should have mentioned that this is a YAML file, so it has a lot of tabs and spaces. I updated the post with the actual format. Apologies for the confusion. – Honshu Feb 06 '19 at 07:26
  • The only way i was able to get this to work was to pipe it 5 times: sed "N;s/\n/\\\n/" config | sed "N;s/\n/\\\n/" | sed "N;s/\n/\\\n/" | sed "N;s/\n/\\\n/" | sed "N;s/\n/\\\n/" – Honshu Feb 06 '19 at 08:41
  • You should not need to pipe your output to another sed command. Please edit your post as it's not clear about what is your expected output (what do you want to do with spaces and tabs?) – SLePort Feb 06 '19 at 08:49
  • Hi SLePort, apologies once again for any confusion (my first question on SO!). I edit my original post. I must maintain all the spaces and tabs. This is a Kubernetes config file, which i have to convert from a file into a string in my bash script in order to send via a remote API call (only accepting strings), – Honshu Feb 07 '19 at 07:17
  • You are a star! Works like a charm! just one quick note: it works perfectly well on my MacOS shell, but for some reason, it hangs on a CentOS bash shell.. not sure why. – Honshu Feb 07 '19 at 08:03
  • Maybe because of Gnu sed on your CentOS. I edited again. – SLePort Feb 07 '19 at 08:16