I am looping million log files, arranged in fixed format: one line with 5 columns:

 0    0.258517     0.20418      0.1592    0.258123

I need to take the number from the 2nd column and save it to the separate log together with the name of the log file, keeping the following format (so the final log should contain N lines for each of the LOG)

name_of_file1: 0.258517
name_of_fileN: nnnnnnn

Here the possible sollution only lacking some function (AWK??) taking the 2nd column value:

   cd foler_with_all_logs
    # do something to extract the number and then add the line with $log_name to >> final_summary.log
    awk '-F, *' '{ printf("%s| %s\n", FILENAME,$2) }' *.log >> final_summary.log

Here the problem in my AWK function which does not recognize the format of the second column of my log file.

  • 2
    Why use -F, *? When you can just print $2 ? Do you have a CSV file? – Inian Oct 23 '20 at 9:56
  • 1
    Are you simply looking for cd folder_with_all_logs; awk '{print $2,FILENAME}' * > final_summary.log? – terdon Oct 23 '20 at 10:05
  • Is the second column repeating inside each log file or you have many different values inside each log file? – thanasisp Oct 23 '20 at 18:15

The file you show doesn't look like it is comma separated, so you don't need (or want) -F,. It looks like all you want is this:

cd folder_with_all_logs 
awk '{print $2,FILENAME}' * > final_summary.log
  • hey, thank you so much ! In my case the following AWK expression did the job awk '-F ' '{ printf("%s| %s\n", FILENAME,$2) }' ./*.log > final_log.txt – user3470313 Oct 23 '20 at 10:25
  • @user3470313 the -F there is useless, the default field delimiter is a space anyway so it isn't doing anything. However, you are now printing $2|FILENAME which is not what your question asked for. I'm glad you found what you need, but next time please make sure your question is actually asking for the format you want. – terdon Oct 23 '20 at 10:44
  • Actually, using this command it prints FILENAME and than the value from $2, as I indicated in my first topic - I've just double checked it ... – user3470313 Oct 23 '20 at 10:57
  • 1
    A million log files (4th word in question)? That * has got to go! Also, making a new .log output file in a directory containing log files will possibly read the output file as input too. Wiser to put the output in ... – Paul_Pedant Oct 23 '20 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.