0

I would like to combine multiple lines into one single line in file using bash. I tried almost all options mentioned in: How do I remove newlines from a text file? but no luck and it didn't remove new lines. This file is JSON output from REST API and parsing using bash.

How to combine below lines into 1 line?

File content (actual):

{"key":"HAM-5765","status":"Closed","components":"Web UX","affectedVersions":"ZCS 8.8.x","fixVersions":"Konrad-Zuse-8.8.10","customerFacingInfo":"[https://bug.rectify?id=35231 Bug 35231] 
* GetEffectiveRightsRequest failed when a delegated admin could not read zimbraMailHost ([https://bug.rectify?id=108536 Bug 108536])
* Had been unable to remove \"Dynamic Group\" from distribution properties page ([https://bug.rectify?id=108499 Bug 108499])
* After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts

File content (expected):

{"key":"HAM-5765","status":"Closed","components":"Web UX","affectedVersions":"ZCS 8.8.x","fixVersions":"Konrad-Zuse-8.8.10","customerFacingInfo":"[https://bug.rectify?id=35231 Bug 35231] * GetEffectiveRightsRequest failed when a delegated admin could not read zimbraMailHost ([https://bug.rectify?id=108536 Bug 108536])* Had been unable to remove \"Dynamic Group\" from distribution properties page ([https://bug.rectify?id=108499 Bug 108499])* After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts

Tried these different commands:

tr -d "\n\r" < yourfile.txt
tr -d '\n' < file.txt
perl -0777 -pe 's/\n+//g' input >output
awk '/[0-9]+/ { a = a $0 ";" } END { print a }' file.txt
perl -p -i -e 's/\R//g;' filename
head -n 1 filename | od -c 
perl -pe 's/\s+//g' yourfile.txt
James Z
  • 12,209
  • 10
  • 24
  • 44
Jitesh Sojitra
  • 3,655
  • 7
  • 27
  • 46
  • 1
    What exactly did not work? `td -d '\n\r'` should work. Could it be, that you expected the file content to change? All of these commands write the processed file to stdout. You have to redirect that output into a new file: `tr -d '\n\r' < inputfile > outputfile`. – Socowi Feb 01 '19 at 10:49
  • It didn't remove new lines. I tried tr -d '\n\r' < inputfile > outputfile and it just moved one line in outputfile. – Jitesh Sojitra Feb 01 '19 at 11:05
  • 1
    I can neither reproduce nor believe that. What is the output of `tr -d '\n\r' < inputfile | wc -l`? I assume it to be `0`. If so, could it be that you are looking at the file using an editor with word wrapping? Please don't use the file on your hard drive, but *copy* your example from this question to make sure that we are using the same input. If it works with your example but not with your actual file, then post a hexdump of your file. – Socowi Feb 01 '19 at 11:07
  • It worked fine using: echo -e "${jsonOutput}" >> ${releaseNoteFile} rm -f ${releaseNoteDir}/ReleaseNote.tmp tr -d '\n\r' < ${releaseNoteFile} > ${releaseNoteDir}/ReleaseNote.tmp mv ${releaseNoteDir}/ReleaseNote.tmp ${releaseNoteFile} Please post as answer so i can accept it. Thanks Socowi! – Jitesh Sojitra Feb 01 '19 at 11:34
  • 1
    It's not clear what your problem was or how @Socowi's comments led you to issue the commands you say resolved the issue. If your real question is "how can I replace the original file with a modified file" there are plenty of duplicates of that. – tripleee Feb 01 '19 at 12:08
  • You can post and accept your own answer. However, I'm with tripleee. Seems more like a combination of two duplicates to me. – Socowi Feb 01 '19 at 12:11
  • The problem was already solved [here](https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed) – Frank Neblung Feb 01 '19 at 13:42

1 Answers1

1

Please try:

cat inputfile | perl -ne 'chomp;print'

Long version:

$ cat inputfile 
{"key":"HAM-5765","status":"Closed","components":"Web UX","affectedVersions":"ZCS 8.8.x","fixVersions":"Konrad-Zuse-8.8.10","customerFacingInfo":"[https://bug.rectify?id=35231 Bug 35231] 
* GetEffectiveRightsRequest failed when a delegated admin could not read zimbraMailHost ([https://bug.rectify?id=108536 Bug 108536])
* Had been unable to remove \"Dynamic Group\" from distribution properties page ([https://bug.rectify?id=108499 Bug 108499])
* After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts
$ cat inputfile | perl -ne 'chomp;print' > outputfile
$ echo $(cat  outputfile) 
{"key":"HAM-5765","status":"Closed","components":"Web UX","affectedVersions":"ZCS 8.8.x","fixVersions":"Konrad-Zuse-8.8.10","customerFacingInfo":"[https://bug.rectify?id=35231 Bug 35231] 123.html 12a.html 12.html 1.html app-12.html inputfile outputfile GetEffectiveRightsRequest failed when a delegated admin could not read zimbraMailHost ([https://bug.rectify?id=108536 Bug 108536])* Had been unable to remove \"Dynamic Group\" from distribution properties page ([https://bug.rectify?id=108499 Bug 108499])* After performing a bulk migration, the Delegated Admin user encountered an `HTTP Error 403` when attempting to download the list of provisioned accounts
Oliver Gaida
  • 1,722
  • 7
  • 14