jeudi 5 mars 2015

How to can I make Filename Sequencing with the help of Loops


my intention is to keep the code as simple as possible.Here there are 5 files namely


A_1.CSV, A_2.CSV, A_3.CSV, A_4.CSV, A_5.CSV


The below code retrieves the first row in a CSV file.


Code : head.sh(filename)



awk -F, 'NR==1 {print $0}' A_1.CSV > Header.csv
awk -F, 'NR==1 {print $0}' A_2.CSV >> Header.csv
awk -F, 'NR==1 {print $0}' A_3.CSV >> Header.csv
awk -F, 'NR==1 {print $0}' A_4.CSV >> Header.csv
awk -F, 'NR==1 {print $0}' A_5.CSV >> Header.csv


Question :


In the above code, only the filename changes from A_1 to A_2 and so on. How can I make the code simple with loops.


Example :



for (i=1;i<=5;i++)
{
A_[i].CSV >> Header.csv
}


I don't know how to do this logic with shell scripting.


UPDATED CODE :


Files in the Directory : /home/thiyagu/Desktop/



for file in 'A_*.CSV'
do
awk -F, 'NR==1 {print $0}' $file >> Newheader.csv
done


Aucun commentaire:

Enregistrer un commentaire