---
title: "linux 合并多行文件"
author: "ronin47"
date: "2016-01-10T10:37:08Z"
tags: []
categories: ["操作系统"]
slug: "linux"
draft: false
lastmod: "2016-01-10T10:37:08Z"
id: 2269629
url: "https://hugoren.iteye.com/blog/2269629"
---

`文件内容`

`cat` `test.txt`

`reportType`

 

`orderId`

 

`sid`

`hospitalName`

`customerName`

`customerSex`

`customerPhone`

`customerId`

`orderNum`

`doctorName`

`recordName`

 

`symptom`

`docSymptom`

`diagnosis`

`checkResult`

`medicine`

`surgery`

`prevTreatment`

`curMedHistory`

`prevMedHistory`

`bodyCheck`

多行合并

1

2

`tr` `-s "\n"` `" "` `< test.txt ;echo`

`reportType orderId sid hospitalName customerName customerSex customerPhone customerId orderNum doctorName recordName symptom docSymptom diagnosis checkResult medicine surgery prevTreatment curMedHistory prevMedHistory bodyCheck medicalOrders check imgs docImages channelId brokerId brokerAvator reportSelf status createdAt updatedAt isDeleted version lock`

方法一合并之后添加，

1

2

`echo` `` `tr`` `-s "\n"` `" "` ``< test.txt;echo` | sed`` `"s/ /,/g"`

`reportType,orderId,sid,hospitalName,customerName,customerSex,customerPhone,customerId,orderNum,doctorName,recordName,symptom,docSymptom,diagnosis,checkResult,medicine,surgery,prevTreatment,curMedHistory,prevMedHistory,bodyCheck,medicalOrders,check,imgs,docImages,channelId,brokerId,brokerAvator,reportSelf,status,createdAt,updatedAt,isDeleted,version,lock`

方法二

1

`echo` `` `sed`` `"/^$/d"` `test.txt | sed` ``"s@\(.*\)@\1,@g"` ``

先删除文件的空行，在添加，然后打印到一行