我试图编写一个shell脚本,在其中可以从给定目录中的文件列表中提取标头和文件名。
例如:ABC_TESTFILE1.csv
包含类似
C1,C2,C3
ABC_TESTFILE2.csv
包含标题
C1,C4,C5
我想输出文本文件为:
ABC_TESTFILE1,C1,C2,C3
ABC_TESTFILE2,C1,C4,C5
我尝试运气是:
#!/bin/bash
# Go to where the files are located
filedir=/home/vikrant_singh_rana/AAA_USP/Combined-Files/*
for filepath in $filedir
do
#echo "Processing $filename"
# do something on $f
var_head=$(head -n 1 basename "$filepath" )
echo "$var_head"
done; > test.txt
您可以printf
用来格式化basename
and的输出head
:
for file in /home/vikrant_singh_rana/AAA_USP/Combined-Files/*.csv; do
printf '%s,%s\n' "$(basename "$file" ".csv")" "$(head -n1 "$file")"
done >test.txt
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句