bash - 如何从 ant 脚本连接文件的所有行?

标签 bash ant

我有一个以下格式的文本文件:

cat yourfile.txt
a
b
c
d
e
f
g

我想把它转换成:

a,b,c,d,e,f,g

这是我现在想到的解决方案:

while read line; do printf "%s%s" $line "," ; done < yourfile.txt
a,b,c,d,e,f,g,

两个问题:

  • 我在结尾处多了一个逗号 (,)
  • 我知道我可以在 shell 脚本中运行此命令并从我的 build.xml 中执行该 shell 脚本。或者有更优雅的解决方案?

最佳答案

你可以使用 sed:

$ sed ':a;N;$!ba;s/\n/,/g' test.txt 
a,b,c,d,e,f,g

参见 this answer阅读更多详情。

否则,对于其他解决方案,删除最后的逗号非常容易。例如 sed :

$ cat test.txt|tr "\n" ","|sed "s/,$//g" 
a,b,c,d,e,f,g
$ while read line; do printf "%s%s" $line "," ; done < test.txt|sed "s/,$//g" # with your solution
a,b,c,d,e,f,g

关于bash - 如何从 ant 脚本连接文件的所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21244518/

相关文章:

testing - Scala 构建工具和测试框架可以很好地协同工作吗?

java - 错误 : JavaFX runtime components are missing, 并且需要运行此应用程序

ant - 如何从属性文件向属性添加任何数字

windows - 在 Windows 上的 bash 中清除 PowerShell 控制台

bash - Ubuntu:重命名不同目录中的多个文件

linux - 防止记录清除命令

linux - 从文件中提取文本的更简单方法

java - 如何指定 Google Guice 的实现路径?

java - Ant 1.9.4 不会使用 Java 8 清理和构建 Netbeans 8.0.2 项目;给出空指针异常

bash - While 循环在 Bash 中的第一行之后停止读取