2 replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
source | link

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 substitutionsput 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 answerArchemar's answer.

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.

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.

1
source | link

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.