linux - 保存 bash 文件的输出

标签 linux bash shell

我在一个文件夹中有一些文件,我需要每个文件夹的第一行

transaction1.csv
transaction2.csv
transaction3.csv
transaction4.csv

我有下一个代码

#All folders that begin with the word transaction

folder='"transaction*"'

ls `echo $folder |sed s/"\""/\/g` >testFiles

# The number of lines of testFiles that is the number of transaction files

num=`cat testFiles | wc -l`

for i in `seq 1 $num`
do
    #The first transaction file
    b=`cat testFiles | head -1`

    #The first line of the first transaction file
    cat `echo $b` | sed -n 1p 

    #remove the first line of the testFiles
    sed -i '1d' testFiles 
done

此代码有效,问题是我需要将每个文件的第一行保存在一个文件中

如果我改变行:

cat `echo $b` | sed -n 1p > salida

它不起作用=(

最佳答案

在庆典中:

for file in *.csv; do head -1 "$file" >> salida; done

正如 Adam 在评论中提到的那样,每次循环都会产生打开文件的开销。如果您需要更好的性能和可靠性,请使用以下内容:

for file in *.csv; do head -1 "$file" ; done > salida

关于linux - 保存 bash 文件的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16744684/

相关文章:

php - 如何查看 Linux 服务器中所有命令的控制台历史记录?

java - Linux 上的运行时执行

linux - 如何在awk中将文件的列名作为变量传递

python - 自动连接到 lxc 容器并在容器内执行命令

bash - 如何使用 Bash 合并 Docker Compose 文件

c - C中的两个进程从同一个文件读取问题

regex - 使用 sed 或 awk,如何从当前行的末尾匹配回指定的字符?

linux - "Syntax error near unexpected token ` crontab '"当尝试在一行中添加 cron 作业作为 sudo 时

android - adb pull -> 未找到设备

linux - 使用不同的参数重新运行先前的命令