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
ls
here. You can just sayfor file in A_*.csv
. – camh Mar 05 '15 at 08:53ls
is not needed. I am getting the result just for one file. In the Header.csv, I can find only one row. But, I need it for multiple files(i.e 5 rows). – Thiyagu Mar 05 '15 at 09:17