如何在Unix中将一行传输到另一行

时间:2013-05-08 04:35:09

标签: linux unix

假设我有以下输入:

BSCTMC             A13728,  J02448,  L13668,  M14730,  A12868,  C11347,
                   L14203,  C02285,  A14419,  BO0797,  S12666,  M12653,
                   D04841,  S02825,  T14713,  L15004,  C01972,  E12057,
                   S13319

我希望A13728,J02448(等等)在同一行上达到S13319。我想创建一个将其保存到文本文件的脚本。谢谢你的帮助。

4 个答案:

答案 0 :(得分:1)

您可以通过以下方式轻松完成此操作:

tr -d '\n' < filename.txt > filename-oneline.txt; mv filename-oneline.txt filename.txt

答案 1 :(得分:1)

当行以逗号(GNU sed)结尾时,你可以用sed替换换行来执行此操作:

sed ':a; /,$/ { N; ba }; s/\n//g' infile

如果您还想从对齐中压缩空格,请将s/, */, /g添加到sed表达式,即:

sed ':a; /,$/ { N; ba }; s/\n//g; s/,   */  /g' infile

输出:

BSCTMC             A13728,  J02448,  L13668,  M14730,  A12868,  C11347,  L14203,  C02285,  A14419,  BO0797,  S12666,  M12653,  D04841,  S02825,  T14713,  L15004,  C01972,  E12057,  S13319

答案 2 :(得分:1)

简单地:

$ xargs < infile > outfile

答案 3 :(得分:0)

如果您不关心中间的空格,可以使用awk

awk '{printf "%s", $0}' inputfile > outputfile

结果:

BSCTMC             A13728,  J02448,  L13668,  M14730,  A12868,  C11347,                   L14203,  C02285,  A14419,  BO0797,  S12666,  M12653,                   D04841,  S02825,  T14713,  L15004,  C01972,  E12057,                   S13319