如何使用bash将文本文件数据写入excel的同一个单元格
我有一个场景,我想将文本文件的所有输出redirect到excel的单个单元格。
文本文件数据:
"remove dead code" "keep cassandra and postgres in OR condition" "InsertFailCDRList" "to be updated in error handling US, write TODO here in comments"
我想要这个数据在我的一列。 例如。
C1 C2 1 TEXT FILE OUTPUT HERE 2 OTHER TEXT FILE OUTPUT HERE
但问题是文本不是来到单个单元格,而是传播到多个单元格。
使用命令:
#!/bin/bash -x output=$(cat commentmessage.txt) number=1 echo $number,"${output}" > buc_review_comments_data3.csv
输出如下所示:
C1 C2 1 "remove dead code" "keep cassandra and postgres in OR coniditon" "InsterFailCDRList"
我希望所有在第1行第2列。我们如何使用bash做到这一点? 请帮忙。
期望的输出:
C1 C2 1 "remove dead code" "keep cassandra and postgres in OR coniditon" "InsterFailCDRList" 2 "new data here" "new data"
所以基本上,我有ID = BUC123
totalcomments = 4
文本文件 – 包含多个注释。
在Excel中需要以上格式。
您可以使用while
循环遍历input文件的内容,然后在每行前加上数字:
#!/bin/bash infile=commentmessage.txt outfile=buc_review_comments_data3.csv number=1 while read line do echo "$number","$line" done < "$infile" > "$outfile"
让我添加一些提示:
- 花括号主要用于将variables名与variables名中允许的其他字符分开,例如:
"${myvar}otherstuff"
。 但是他们也可以提高你的代码的可读性。 在你的例子中,你可以echo "$number,$line"
(因为在variables名中是不允许的),但是echo "${number},${line}
会很好,清楚你的variables名是,而保存单独的报价。 - 虽然在您的示例中没有必要引用
$number
,但作为最佳做法, 您应始终对variables扩展进行双引号引用 。 - 如果您只是从文件中读取,请使用redirect(
<
)而不是cat
,请参阅无用的cat 。