1

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
Thiyagu
  • 111

3 Answers3

1

what about

 awk 'FNR==1' A_*.csv > Header.csv

where

  • FNR is File Number Record
  • default action is to print the while line
  • I dropped -F, as you don't care about individual field (If you have other thing to do, however, you can add it back)
Archemar
  • 31,554
-1

Something like this will work:

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

This is basic shell scripting looping. You can find these tutorials online, if you search for them.

rahul
  • 1,181
-1

Single quotes prevent expansion: every character in a single-quoted string is interpreted literally (except ' itself which ends the string). So when you want * to act as a wildcard, leave it outside the quotes. (Double quotes also prevent * from acting as a wildcard.)

for file in A_*.CSV …

The rest of your updated code is probably ok, but you should put double quotes around variable substitutions, otherwise sooner or later it will bite you.

You can take the redirection outside the loop. This is slightly faster.

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

This snippet appends to Newheader.csv. If you want to overwrite the file when it already exists, like in your original code, replace >> by >.

There are several ways to simplify your script, if all you want is to print the first line of several files. Since you're just printing the first line, -F, isn't used. Furthermore you could use head -n 1 instead of awk 'NR == 1 {print $0}'. On Linux (but not on all Unix variants), to print just the first line of several files, you can use head without any loop:

head -q -n 1 A_*.CSV >Newheader.csv

You also don't need a loop with awk, see Archemar's answer.