0

can you please give me a hand with this code which I'm using to build a log?

##Log construction
printf $timestamp >> $temp"logs.txt"
printf "        " >> $temp"logs.txt"
printf $filekey >> $temp"logs.txt"
cat $temp$filekey"_content.txt" >> $temp"logs.txt"

This code is into a bash script that contains a loop which is running N times depending on the quantity of lines of certain text file (in this example that text files has 2 lines only), the thing is that the order of the results printed in logs.txt is OK just the first time that this script runs, like here:

2020_09_04--10:09:41    myhost1.ec2.local        records_found         62
2020_09_04--10:09:41    myhost2.ec2.local        records_found         62

Next time the script runs it amends the logs.txt file but paste results right after the last text found which is spoiling the log format, like here:

2020_09_04--10:09:41    myhost1.ec2.local        records_found         62
2020_09_04--10:09:41    myhost2.ec2.local        records_found         622020_09_04--10:10:21    myhost1.ec2.local        records_found         64
2020_09_04--10:10:21     myhost2.ec2.local        records_found         64

The expected results are:

    2020_09_04--10:09:41    myhost1.ec2.local        records_found         62
    2020_09_04--10:09:41    myhost2.ec2.local        records_found         62
    2020_09_04--10:10:21    myhost1.ec2.local        records_found         64
    2020_09_04--10:10:21    myhost2.ec2.local        records_found         64

Any help is highly appreciated. Thanks. -Alex.

Alex
  • 25
  • 2
  • 1
    `printf`'s first argument is a _format string_, not a literal string to print. – Charles Duffy Sep 04 '20 at 17:36
  • 1
    If you want to pass it a literal string, then it should be generally used like `printf '%s\n' "$string_to_print"`. And notice the quotes -- they're not optional if you want correct results. – Charles Duffy Sep 04 '20 at 17:36
  • 1
    On which point... I'm about to close this as a duplicate of a bug that's written up about `echo`, but caused by not using quotes; you have the `printf` analogue to that bug; no matter which command you're running, when you have spaces in your values, it's mandatory to use quotes to prevent munging.. – Charles Duffy Sep 04 '20 at 17:37
  • hello, I ran your recommendations but it is not completely fixed, now I get extra spaces between lines which still does not give me the right format ... thanks – Alex Sep 04 '20 at 18:18
  • If those "extra spaces" are additional newlines because your original data already contains newlines, change `'%s\n'` to just `%s`. I'd need a [mre] to say more -- you're showing us output, but not providing sample values to your variables. – Charles Duffy Sep 04 '20 at 18:24
  • BTW, consider `printf '%s %s\n' "$timestamp" "$filekey" >>yourfile` to write both values (and a space between them) at once. You can also tell printf to pad your fields out to a certain length by adding spaces itself; `%-45s` or `%45s`, for example, will do that (making something be a 45-character field). – Charles Duffy Sep 04 '20 at 18:24
  • ...and don't repeat `>>yourfile` line-after-line: every time you use that you're opening the output file, and closing it again. You can redirect stdout for a whole block of lines: `{ cmd1; cmd2; cmd3; } >>yourfile` will open `yourfile` for write _just once_, and send output of all three of `cmd1`, `cmd2` and `cmd3` to it. – Charles Duffy Sep 04 '20 at 18:25
  • thanks for share those best practices, problem is fixed now. the issue was not with variables values but with the format presented on the log.txt file. - the issue has been fixed by adding an /n to the log.txt file out of the loop. thanks. – Alex Sep 04 '20 at 18:49

0 Answers0