简体   繁体   English

Cat一个.csv文件与目录中的所有其他文件

[英]Cat one .csv file with all other files in directory

I have multiple csv files that each needs header data placing in row 1, from a seperate csv file which is 1 line in length. 我有多个csv文件,每个文件需要放置在第1行的标题数据,来自一个长度为1行的单独csv文件。 The file is called Anon_student_ID, and every other file in the directory needs this data as line 1 while retaining it's file name. 该文件名为Anon_student_ID,目录中的每个其他文件都需要此数据作为第1行,同时保留其文件名。

I am trying to use the cat function from the command line. 我试图从命令行使用cat函数。

My attempted to solution was - $ cat Anon_student_ID.csv *.csv > *.csv 我试图解决的问题是 - $ cat Anon_student_ID.csv *.csv > *.csv

However, I get bash: *.csv: ambiguous redirect , 但是,我得到了bash: *.csv: ambiguous redirect

Could someone explain why it isn't taking the second filename and using it as the output file name? 有人可以解释为什么它没有采用第二个文件名并将其用作输出文件名吗? And what would be a better way of doing this. 什么是更好的方式来做到这一点。

Thanks 谢谢

#!/bin/bash
shopt -s nullglob
shopt -s extglob
TEMP=$(mktemp)
IFS= read -r HEADER < Anon_student_ID.csv
for CSV in !(Anon_student_ID).csv; do
    { echo "$HEADER"; cat "$CSV"; } > "$TEMP" && cat "$TEMP" > "$CSV"
done
rm -f "$TEMP"
  • nullglob prevents patterns from presenting themselves when no match is found. nullglob可防止模式在未找到匹配项时显示。
  • extglob enables extended patterns like !(...) . extglob支持扩展模式,如!(...)
  • !(Anon_student_ID).csv matches all CSV files not having the filename Anon_student_ID . !(Anon_student_ID).csv匹配所有没有文件名Anon_student_ID CSV文件。
  • sed is good but it will look hacky on this. sed很好,但它会看起来很hacky。 It would require repeated reading of the header file as well. 这也需要重复读取头文件。

If files are not that big, using an array for a buffer instead of a temporary file would be good too: 如果文件不是那么大,使用数组作为缓冲区而不是临时文件也是好的:

#!/bin/bash
shopt -s nullglob
shopt -s extglob
[[ BASH_VERSINFO -ge 4 ]] || exit 1  ## Make sure we have `readarray`
IFS= read -r HEADER < Anon_student_ID.csv
for CSV in !(Anon_student_ID).csv; do
    readarray -t LINES < "$CSV"
    printf '%s\n' "$HEADER" "${LINES[@]}" > "$CSV"
done

As a one-liner: 作为单线:

mv Anon_student_ID.csv Anon_student_ID; for i in *.csv; do cat Anon_student_ID > tmpfile; cat ${i} >> tmpfile; mv tmpfile ${i}; done; mv Anon_student_ID Anon_student_ID.csv

In a readable format: 以可读格式:

#!/bin/bash
mv Anon_student_ID.csv Anon_student_ID
for i in *.csv; do 
    cat Anon_student_ID > tmpfile
    cat ${i} >> tmpfile
    mv tmpfile ${i}
done
mv Anon_student_ID Anon_student_ID.csv

Anon_student_ID is renamed, so it will not be used for appending itself to itself, since it would match *.csv , too. Anon_student_ID被重命名,因此它不会用于将自身附加到自身,因为它也会匹配*.csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对脚本文件的参数中给定目录中的所有文件运行cat命令,并以名称作为第二个参数输出 - run cat command for all the files in the directory given in argument of the script file and out put with the name given as second argument 将所有文件递归到单个文件中 - Recursive cat all the files into single file cat - 没有这样的文件或目录 - cat - No such file or directory 除了一个之外的 Cat 文件 - Cat files except one 如何删除目录中除一个文件夹和一个文件以外的所有文件? - how to delete all files in directory except one folder and one file? 将多个文件分类为一个文件,文件名位于数据之前 - Cat several files into one file with the file name before the data 分类所有文件并在字符前添加标签作为文件名 - Cat all files and add a tag as the file name before a character Cat命令给出“没有这样的文件或目录” - Cat command giving “ No such file or directory” 是否有 linux 命令循环遍历目录中的文件,然后将所有文件名和内容写入 csv 文件? - Is there a linux command loop through files in a directory then write all files names and content into csv file? 我想为一个文件列出目录名称,然后为每个结果搜索目录并移动找到的所有文件 - I want to cat a file for a list of file names, then search a directory for each of the results and move any files it finds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM