将文本文件转换为逗号分隔的字符串

时间:2018-10-31 23:54:40

标签: bash csv awk sed delimited-text

我似乎找不到与这个确切问题相匹配的SO问题。

我有一个文本文件,每行有一个文本标记,没有任何逗号,制表符或引号。我想根据文件内容创建一个逗号分隔的字符串。

输入:

one
two
three

输出:

one,two,three

我正在使用以下命令:

csv_string=$(tr '\n' ',' < file | sed 's/,$//')

有没有更有效的方法?

5 个答案:

答案 0 :(得分:6)

执行此操作的常用命令是paste

csv_string=$(paste -sd, file.txt)

答案 1 :(得分:3)

您可以完全使用bash参数扩展运算符来完成此操作,而不是使用trsed

csv_string=$(<file)               # read file into variable
csv_string=${csv_string//$'\n'/,} # replace \n with ,
csv_string=${csv_string%,}        # remove trailing comma

答案 2 :(得分:2)

使用Awk的一种方法是重置RS并将记录视为空白行。这样可以处理带空格的单词,并按预期将其格式化为CSV格式。

awk '{$1=$1}1' FS='\n' OFS=',' RS= file

{$1=$1}是一种基于对字段($0)和/或记录分隔符({{1的修改)来重构文件的每一行(FS/OFS)中的字段的方法。 }})。尾随的RS/ORS将打印在1内部完成的每一行。

答案 3 :(得分:1)

使用Perl单线版:

$ cat csv_2_text
one
two
three
$ perl -ne '{ chomp; push(@lines,$_) } END { $x=join(",",@lines);  print "$x" }' csv_2_text
one,two,three

$ perl -ne ' { chomp; $_="$_," if not eof ;printf("%s",$_) } ' csv_2_text
one,two,three
$

来自@codeforester

$ perl -ne 'BEGIN { my $delim = "" } { chomp; printf("%s%s", $delim, $_); $delim="," } END { printf("\n") }' csv_2_text
one,two,three
$

答案 4 :(得分:0)

在Linux机器上测试了四种方法-Bash onlypasteawkPerl,以及问题中显示的npm WARN eslint-config-react-app@2.1.0 requires a peer of babel-eslint@^7.2.3 but none is installed. You must install peer dependencies yourself. npm WARN eslint-config-react-app@2.1.0 requires a peer of eslint@^4.1.1 but none is installed. You must install peer dependencies yourself. npm WARN eslint-loader@1.9.0 requires a peer of eslint@>=1.6.0 <5.0.0 but none is installed. You must install peer dependencies yourself. npm WARN eslint-plugin-jsx-a11y@5.1.1 requires a peer of eslint@^2.10.2 || ^3 || ^4 but none is installed. You must install peer dependencies yourself. npm WARN firebase-functions@2.1.0 requires a peer of firebase-admin@~6.0.0 but none is installed. You must install peer dependencies yourself. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})方法:

tr | sed

令人惊讶的是,仅Bash解决方案的效果很差。 #!/bin/bash # generate test data seq 1 10000 > test.file times=${1:-50} printf '%s\n' "Testing paste solution" time { for ((i=0; i < times; i++)); do csv_string=$(paste -sd, test.file) done } printf -- '----\n%s\n' "Testing pure Bash solution" time { for ((i=0; i < times; i++)); do csv_string=$(<test.file) # read file into variable csv_string=${csv_string//$'\n'/,} # replace \n with , csv_string=${csv_strings%,} # remove trailing comma done } printf -- '----\n%s\n' "Testing Awk solution" time { for ((i=0; i < times; i++)); do csv_string=$(awk '{$1=$1}1' FS='\n' OFS=',' RS= test.file) done } printf -- '----\n%s\n' "Testing Perl solution" time { for ((i=0; i < times; i++)); do csv_string=$(perl -ne '{ chomp; $_="$_," if not eof; printf("%s",$_) }' test.file) done } printf -- '----\n%s\n' "Testing tr | sed solution" time { for ((i=0; i < times; i++)); do csv_string=$(tr '\n' ',' < test.file | sed 's/,$//') done } 排在最前面,接着是pastetr | sedAwk

perl

由于某些原因,Testing paste solution real 0m0.109s user 0m0.052s sys 0m0.075s ---- Testing pure Bash solution real 1m57.777s user 1m57.113s sys 0m0.341s ---- Testing Awk solution real 0m0.221s user 0m0.152s sys 0m0.077s ---- Testing Perl solution real 0m0.424s user 0m0.388s sys 0m0.080s ---- Testing tr | sed solution real 0m0.162s user 0m0.092s sys 0m0.141s 挂在运行Bash 4.4.23的macOS Mojave上。


相关帖子: