Nico
Nico

Reputation: 11

Bash merging files

I will merge multiples .csv Reports.

When i download it will write like this:

File 1.

Name Name Flavor
AAA  BBB  CCC
AAA  BBB  CCC

File 2.

Name Name Flavor
ACB  DHB  123

I tried with the "awk" command.

awk 'FNR== 1{print ""}1' *.csv >> ReportsVol.csv 

With them it merged all files. But the output is then the follwing.

Name Name Flavor
AAA  BBB  CCC
AAA  BBB  CCC
Name Name Flavor
ACB  DHB  123

But i want that between each file a date is. So i think this will possible with the "timestamp" but i don't know how to deploy them. And the description (Name,Name,Flavor) should be just once written in the top of the file.

In the end i want something like this:

Name Name Flavor
 <timestamp>
AAA  BBB  CCC
ABC  BCD  CDE

<timestamp>
ACB  DHB  123

Thanks in advance for your help!

Upvotes: 1

Views: 100

Answers (3)

Corentin Limier
Corentin Limier

Reputation: 5006

systime() will work if you use GNU awk but you would probably replace this function by the one which returns the date you need.

awk 'NR==1;FNR==1{print systime(); next}1' file1 file2

Returns

Name Name Flavor
1543486733
AAA  BBB  CCC
AAA  BBB  CCC
1543486733
ACB  DHB  123

I have no idea what kind of date you want so I used systime()


EDIT

Use the time of last modification of file :

awk 'NR==1{printf "%s", $0};FNR==1{print ""; system("date -r" FILENAME); next}1' *.csv > ReportsVol.csv

Returns :

Name Name Flavor
Thu Nov 29 11:11:49 CET 2018
AAA  BBB  CCC
AAA  BBB  CCC

Thu Nov 29 11:12:07 CET 2018
ACB  DHB  123

Use system("date +%s -r " FILENAME) if you want the date format in seconds since epoch.

Remove print ""; if you do not need the empty line separating the files.

Upvotes: 4

Ed Morton
Ed Morton

Reputation: 203532

awk -v d="$(date)" 'FNR==1{print (NR==1 ? $0 : "") ORS d; next} 1' *.csv

Specify the date options as you like.

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133518

could you please try following.

awk '
FNR==1{
  if(++count==1){
     print $0 ORS systime()
  }
  if(count==2){
     print systime()
  }
  next
}
1'   Input_file

Upvotes: 1

Related Questions