从txt文件制作csv

发布于 2024-10-05 07:53:02 字数 232 浏览 3 评论 0原文

我有很多这样的 txt 文件:

Title 1
Text 1(more then 1 line)

我想从所有这些文件中制作一个 csv 文件,它看起来像这样:

Title 1,Text 1
Title 2,Text 2
Title 3,Text 3
etc

我该怎么做?我认为 awk 很有用,但不知道如何实现它。

I have a lot of txt files like this:

Title 1
Text 1(more then 1 line)

And I would like to make one csv file from all of them that it will look like this:

Title 1,Text 1
Title 2,Text 2
Title 3,Text 3
etc

How could I do it? I think that awk is good for it but don't know how to realize it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮坠入山谷 2024-10-12 07:53:02

我可以建议:

paste -d, file1 file2 file3

要处理大量文件,每个输出文件最多 40 个(未经测试,但接近):

xargs -n40 files... echo >tempfile
num=1
for line in $(<tempfile)
do
    paste -d, $line >outfile.$num
    let num=num+1
done

May I suggest:

paste -d, file1 file2 file3

To handle large numbers of files, max 40 per output file (untested, but close):

xargs -n40 files... echo >tempfile
num=1
for line in $(<tempfile)
do
    paste -d, $line >outfile.$num
    let num=num+1
done
若有似无的小暗淡 2024-10-12 07:53:02

这大约是您发布的内容,并进行了一些改进。

for text in *
do
    awk 'BEGIN {q="\""; print q}
         NR==1 {
                gsub(" "," ")    # why?
                gsub("Title: *","")
                print
               }
         NR>1  {
                gsub(" "," ")    # why?
                gsub("Content: *","")
                gsub(q,q q)
                print
               }

         END {print q}' "$text" >> ../final
done

编辑:

如果您有一堆仅包含两行的文件,请尝试以下操作:

sed 'N;s/\n/,/' file*.txt

如果每个文件包含两行以上,那么它将把每对行放在同一行上,并用逗号。

This is approximately what you posted with some improvements.

for text in *
do
    awk 'BEGIN {q="\""; print q}
         NR==1 {
                gsub(" "," ")    # why?
                gsub("Title: *","")
                print
               }
         NR>1  {
                gsub(" "," ")    # why?
                gsub("Content: *","")
                gsub(q,q q)
                print
               }

         END {print q}' "$text" >> ../final
done

Edit:

If you have a bunch of files that consist of only two lines, try this:

sed 'N;s/\n/,/' file*.txt

If the files contain more than two lines each then it will put each pair of lines on the same line separated by a comma.

找个人就嫁了吧 2024-10-12 07:53:02

给定 3 个包含以下数据的文件:

file1.txt

Heading 1
Text 1
Text 2

file2.txt

Heading 2
Text 1

file3.txt

Heading 3
Text 1
text 2
Text 3

预期结果是:

Heading 1,Text 1,Text 2 
Heading 2,Text1 
Heading 3,Text 1,text 2,Text 3

这是使用程序 createcsv 完成的下面的 .awk 被调用为

gawk -f createcsv.awk file1.txt file2.txt file3.txt

createcsv.awk

{
  if (1 == FNR) {
     # It is the first line of a new file
     if (csvline != "") {
       # First file or empty files we can ignore
       print csvline;
     }
     csvline = "";
     delimiter = "";
  }
  csvline = csvline delimiter $0;
  if ("" == delimiter) { delimiter="," }
}
END{
 print csvline;
}

Given 3 files containing the following data:

file1.txt

Heading 1
Text 1
Text 2

file2.txt

Heading 2
Text 1

file3.txt

Heading 3
Text 1
text 2
Text 3

The expected results are:

Heading 1,Text 1,Text 2 
Heading 2,Text1 
Heading 3,Text 1,text 2,Text 3

This is accomplished using the program createcsv.awk below invoked as

gawk -f createcsv.awk file1.txt file2.txt file3.txt

createcsv.awk

{
  if (1 == FNR) {
     # It is the first line of a new file
     if (csvline != "") {
       # First file or empty files we can ignore
       print csvline;
     }
     csvline = "";
     delimiter = "";
  }
  csvline = csvline delimiter $0;
  if ("" == delimiter) { delimiter="," }
}
END{
 print csvline;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文