Skip to main content
edited tags
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k

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 

Thank You

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 

Thank You

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 
added 179 characters in body
Source Link
Thiyagu
  • 111
  • 3

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 : Thank/home/thiyagu/Desktop/

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

Thank You

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. Thank You

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 

Thank You

edited title
Link
Thiyagu
  • 111
  • 3

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

Source Link
Thiyagu
  • 111
  • 3
Loading