bash shell 用逗号替换 cr\lf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4594319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:01:56  来源:igfitidea点击:

shell replace cr\lf by comma

bashshellnewlinetext-processing

提问by vinnitu

I have input.txt

我有 input.txt

1
2
3
4
5

I need to get such output.txt

我需要得到这样的 output.txt

1,2,3,4,5

How to do it?

怎么做?

回答by eumiro

Try this:

尝试这个:

tr '\n' ',' < input.txt > output.txt

回答by Jonathan Leffler

With sed, you could use:

使用sed,您可以使用:

sed -e 'H;${x;s/\n/,/g;s/^,//;p;};d'

The Happends the pattern space to the hold space (saving the current line in the hold space). The ${...}surrounds actions that apply to the last line only. Those actions are: xswap hold and pattern space; s/\n/,/gsubstitute embedded newlines with commas; s/^,//delete the leading comma (there's a newline at the start of the hold space); and pprint. The ddeletes the pattern space - no printing.

H追加模式空间到保持空间(节省保留空间当前行)。${...}仅适用于最后一行的环绕动作。这些操作是:x交换保持和模式空间;s/\n/,/g用逗号替换嵌入的换行符;s/^,//删除前导逗号(保留空间的开头有一个换行符);并p打印。在d删除模式空间-不打印。

You could also use, therefore:

您也可以使用,因此:

sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}'

The -nsuppresses default printing so the final dis no longer needed.

-n禁止显示默认打印所以最终d不再需要。

This solution assumes that the CRLF line endings are the local native line ending (so you are working on DOS) and that sedwill therefore generate the local native line ending in the print operation. If you have DOS-format input but want Unix-format (LF only) output, then you have to work a bit harder - but you also need to stipulate this explicitly in the question.

此解决方案假定 CRLF 行结尾是本地本地行结尾(因此您在 DOS 上工作),sed因此将在打印操作中生成本地本地行结尾。如果您有 DOS 格式的输入但想要 Unix 格式(仅 LF)输出,那么您必须更努力地工作 - 但您还需要在问题中明确规定这一点。

It worked OK for me on MacOS X 10.6.5 with the numbers 1..5, and 1..50, and 1..5000 (23,893 characters in the single line of output); I'm not sure that I'd want to push it any harder than that.

它在 MacOS X 10.6.5 上运行正常,数字为 1..5、1..50 和 1..5000(单行输出中有 23,893 个字符);我不确定我是否想要更努力地推动它。

回答by glenn Hymanman

In response to @Jonathan's comment to @eumiro's answer:

回应@Jonathan 对@eumiro 的回答的评论:

tr -s '\r\n' ',' < input.txt | sed -e 's/,$/\n/' > output.txt

回答by Arnaud

trand sedused be very good but when it comes to file parsing and regex you can't beat perl (Not sure why people think that sed and tr are closer to shell than perl... )

tr并且sed使用非常好,但是当涉及到文件解析和正则表达式时,您无法击败 perl(不知道为什么人们认为 sed 和 tr 比 perl 更接近 shell ......)

perl -pe 's/\n/,/' your_file

if you want pure shell to do it then look at string matching

如果你想要纯 shell 来做,那么看看字符串匹配

${string/#substring/replacement}

回答by Nestor Urquiza

Use paste command. Here is using pipes:

使用粘贴命令。这是使用管道:

echo "1\n2\n3\n4\n5" | paste -s -d, /dev/stdin

Here is using a file:

这是使用文件:

echo "1\n2\n3\n4\n5" > /tmp/input.txt
paste -s -d, /tmp/input.txt

Per man pages the s concatenates all lines and d allows to define the delimiter character.

每个手册页 s 连接所有行, d 允许定义分隔符。

回答by gsbabil

  • Awk versions:
    • awk '{printf("%s,",$0)}' input.txt
    • awk 'BEGIN{ORS=","} {print $0}' input.txt
    • Output - 1,2,3,4,5,
  • awk 版本:
    • awk '{printf("%s,",$0)}' input.txt
    • awk 'BEGIN{ORS=","} {print $0}' input.txt
    • 输出 - 1,2,3,4,5,

Since you asked for 1,2,3,4,5, as compared to 1,2,3,4,5,(note the comma after 5, most of the solutions above also include the trailing comma), here are two more versions with Awk (with wcand sed) to get rid of the last comma:

由于您要求1,2,3,4,5, 与1,2,3,4,5,(注意 5 之后的逗号,上面的大多数解决方案还包括尾随逗号)相比,这里还有两个版本的 Awk(带有wcsed)以摆脱最后一个逗号:

  • i='input.txt'; awk -v c=$(wc -l $i | cut -d' ' -f1) '{printf("%s",$0);if(NR<c){printf(",")}}' $i

  • awk '{printf("%s,",$0)}' input.txt | sed 's/,\s*$//'

  • i='input.txt'; awk -v c=$(wc -l $i | cut -d' ' -f1) '{printf("%s",$0);if(NR<c){printf(",")}}' $i

  • awk '{printf("%s,",$0)}' input.txt | sed 's/,\s*$//'

回答by qneill

  • python version:

    python -c 'import sys; print(",".join(sys.stdin.read().splitlines()))'

  • 蟒蛇版本:

    python -c 'import sys; print(",".join(sys.stdin.read().splitlines()))'

Doesn't have the trailing comma problem (because joinworks that way), and splitlinessplits data on native line endings (and removes them).

没有尾随逗号问题(因为那样join工作),并splitlines在本机行结尾处拆分数据(并删除它们)。

回答by Noel Yap

cat input.txt | sed -e 's|$|,|' | xargs -i echo "{}"