如何使用bash将文本文件数据写入Excel的同一单元格 [英] How to write text file data into same cell of excel using bash

查看:209
本文介绍了如何使用bash将文本文件数据写入Excel的同一单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



文本文件数据:

 删除死代码
将cassandra和postgres保留在OR条件
InsertFailCDRList
在美国的错误处理中进行更新,在评论中写下TODO

我希望这个数据在我的一列。
对于例如

  C1 C2 
1文本文件输出此处
2其他文本文件OUTPUT HERE

但问题是文本不是单细胞,而是传播到多个单元格。 >

使用命令:

 #!/ bin / bash -x 
output = $(cat commentmessage.txt)
number = 1
echo $ number,$ {output}> buc_review_comments_data3.csv

输出如下:

  C1 C2 
1删除死代码
将cassandra和postgres保留在OR分配
InsterFailCDRList

我想要第1列第2列。我们如何使用bash?请帮助。



期望的输出:

  C1 C2 
1删除死码
将cassandra和postgres保留在OR分配中
InsterFailCDRList

2这里的新数据
新数据



所以基本上,我有
ID = BUC123



totalcomments = 4



想要这些以上格式的excel。

解决方案

您可以使用 while 循环遍历输入文件的内容,然后在每行前面加上您的号码:

 #!/ bin / bash 
infile = com mentmessage.txt
outfile = buc_review_comments_data3.csv
number = 1

while read line
do
echo$ number,$ line
完成$ infile> $ outfile

让我加一些提示:




  • 大括号主要用于将变量名与双引号中的变量名中允许的其他字符分开,例如:$ {myvar} otherstuff。但是它们也可以增强代码的可读性。在您的示例中,您可以执行 echo$ number,$ line(因为不允许变量名),但 echo$ {number},$ {line} 会很好和清楚你的变量名称,同时保存单独的引用。 >
  • 虽然在您的示例中不需要引用 $ number ,但作为最佳实践,你应该总是双引号你的变量扩展

  • 如果你只是从一个文件,使用重定向( <)而不是 cat ,请参阅无用的猫


I have a scenario where I want to redirect all the output of a text file into a single cell of excel.

text file data:

"remove dead code" 
"keep cassandra and postgres in OR condition"
"InsertFailCDRList"
"to be updated in error handling US, write TODO here in comments"

I want this data in my one column. For eg.

    C1             C2
    1            TEXT FILE OUTPUT HERE
    2            OTHER TEXT FILE OUTPUT HERE

But problem is text is not coming single cell but it spreads to multiple cells.

Using commands:

#!/bin/bash  -x
output=$(cat commentmessage.txt)
number=1
echo $number,"${output}" > buc_review_comments_data3.csv

Output is like:

        C1             C2
         1            "remove dead code"
        "keep cassandra and postgres in OR coniditon"
        "InsterFailCDRList"

I want all under row 1 column 2. How can we do this using bash? Please help.

Desired Output:

       C1                C2
        1            "remove dead code"
                     "keep cassandra and postgres in OR coniditon"
                      "InsterFailCDRList"

        2             "new data here"
                      "new data"

So Basically, I have ID=BUC123

totalcomments=4

Text file - containing multiple comments.

Want these in above format in excel.

解决方案

You can use a while loop to iterate over the contents of your input file, and then prefix each line with your number:

#!/bin/bash
infile=commentmessage.txt
outfile=buc_review_comments_data3.csv
number=1

while read line
do
    echo "$number","$line"
done < "$infile" > "$outfile"

Let me add a few hints:

  • Curly braces are mainly useful to separate variable names from other characters that are allowed in variable names inside double quotes, e.g.: "${myvar}otherstuff". But they can also enhance readability of your code. In your example, you could do echo "$number,$line" as well (because , is not allowed in variable names), but echo "${number},${line} would be nice and clear about what your variable names are, while saving the separate quoting.
  • While it is not necessary to quote $number in your example, as a best-practice, you should always double-quote your variable expansions.
  • If you just read from a file, use redirection (<) instead of cat, see Useless Use of cat.

这篇关于如何使用bash将文本文件数据写入Excel的同一单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆