我在一个文件夹中有一些文件,我需要每个文件夹的第一行
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/