使用bash将多行合并为文件中的一行

时间:2019-02-01 10:36:27

标签: bash

我想使用bash在文件中将多行合并为一行。我尝试了几乎所有提到的选项: How do I remove newlines from a text file?,但没有运气,也没有删除新行。此文件是REST API的JSON输出,并使用bash进行解析。

如何下面线合并成1行?

文件内容(实际):

{"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

文件内容(预期):

{"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

尝试了以下不同的命令:

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

1 个答案:

答案 0 :(得分:1)

请尝试:

cat inputfile | perl -ne 'chomp;print'

长版:

$ 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