2017-08-17 4 views
0

Ich verkette mehrere CSV-Dateien mit awk und Ändern der Zeilen aber ich möchte nicht die Header zu drucken ich kümmere mich um die Header in einer anderen Methode Ich möchte nur awk die Header überspringenawk mehrere CSV-Datei überspringen Header

hier ist mein Code

OutFileName="Final.csv"      # Fix the output name 
i=0          # Reset a counter 
for filename in ./*.csv; do 
if [ "$filename" != "$OutFileName" ] ;  # Avoid recursion 
then 
    if [[ $i -eq 0 ]] ; then 
     head -1 $filename > $OutFileName # Copy header if it is the first file 
    fi 
    text=$(echo $filename | cut -d '_' -f 2-) #spilliting the string to get the date 
    text=$(echo $text | cut -d '.' -f -1) 
    awk -F',' -v txt="$text" 'FNR>1||$0=txt" "$1 FS $0' OFS=',' $filename | cat >> $OutFileName # cocatinating with awk 
    i=$(($i + 1)) #increse number 
fi 
done 

es eine Ausgabe wie diese Sie awk nimmt

Trialtime type Track time Notes Athlete 
Trialtime type Track time Notes Athlete 
20170101 07:15:00 07:15:00 Warmup ABC 85.2 Initial warmup Jon 
20170101 07:45:00 07:45:00 Sprint1 ABC 59.44 First Sprint Jon 
20170101 08:30:00 08:30:00 TRIAL ABC 57.21 Final attempt Jon 
20170101 08:00:00 08:00:00 Warmup ABC 120.51 Initial warmup Bill 
20170101 08:40:05 08:40:05 Sprint1 ABC 61.35 First Sprint Bill 
20170101 09:15:00 09:15:00 Sprint2 ABC 60.08 Second Sprint Bill 
20170101 10:30:00 10:30:00 TRIAL ABC 60.37 Final attempt Bill 
20170101 07:15:00 07:15:00 Warmup ABC 85.2 Initial warmup Jon 
20170101 07:45:00 07:45:00 Sprint1 ABC 59.44 First Sprint Jon 
20170101 08:30:00 08:30:00 TRIAL ABC 57.21 Final attempt Jon 
20170101 08:00:00 08:00:00 Warmup ABC 120.51 Initial warmup Bill 
20170101 08:40:05 08:40:05 Sprint1 ABC 61.35 First Sprint Bill 
20170101 09:15:00 09:15:00 Sprint2 ABC 60.08 Second Sprint Bill 
20170101 10:30:00 10:30:00 TRIAL ABC 60.37 Final attempt Bill 
Trialtime type Track time Notes Athlete 
201701023 07:15:00 07:15:00 Warmup ABC 85.2 Initial warmup Jon 
201701023 07:45:00 07:45:00 Sprint1 ABC 59.44 First Sprint Jon 
201701023 08:30:00 08:30:00 TRIAL ABC 57.21 Final attempt Jon 
201701023 08:00:00 08:00:00 Warmup ABC 120.51 Initial warmup Bill 
201701023 08:40:05 08:40:05 Sprint1 ABC 61.35 First Sprint Bill 
201701023 09:15:00 09:15:00 Sprint2 ABC 60.08 Second Sprint Bill 
201701023 10:30:00 10:30:00 TRIAL ABC 60.37 Final attempt Bill 
201701023 07:15:00 07:15:00 Warmup ABC 85.2 Initial warmup Jon 
201701023 07:45:00 07:45:00 Sprint1 ABC 59.44 First Sprint Jon 
201701023 08:30:00 08:30:00 TRIAL ABC 57.21 Final attempt Jon 
201701023 08:00:00 08:00:00 Warmup ABC 120.51 Initial warmup Bill 
201701023 08:40:05 08:40:05 Sprint1 ABC 61.35 First Sprint Bill 
201701023 09:15:00 09:15:00 Sprint2 ABC 60.08 Second Sprint Bill 
201701023 10:30:00 10:30:00 TRIAL ABC 60.37 Final attempt Bill 

gibt den Header jedes Mal für jeden cs sehen v Datei gibt es eine Möglichkeit, die Header zu ignorieren?

Antwort

1

In Ihrem Code, fügen Sie einfach NR!=1 &&:

OutFileName="Final.csv"      # Fix the output name 
i=0          # Reset a counter 
for filename in ./*.csv; do 
if [ "$filename" != "$OutFileName" ] ;  # Avoid recursion 
then 
    if [[ $i -eq 0 ]] ; then 
     head -1 $filename > $OutFileName # Copy header if it is the first file 
    fi 
    text=$(echo $filename | cut -d '_' -f 2-) #spilliting the string to get the date 
    text=$(echo $text | cut -d '.' -f -1) 
    awk -F',' -v txt="$text" 'FNR>1||NR!=1 && $0=txt" "$1 FS $0' OFS=',' $filename | cat >> $OutFileName # cocatinating with awk 
    i=$(($i + 1)) #increse number 
fi 
done 

Dies wird die erste Zeile überspringen.

1

Ändern || mit {} und mit explizitem Druck

awk -F',' -v txt="$text" 'FNR>1 { print txt" "$1 FS $0 }'