An alternative is to use awk
with the command printf
, since it prints the content of each line, but without adding the line break:
awk '{printf $0 " ";}' psResults.csv
With this all the lines of the file are united to stay all in one line, separated by space. To do everything in the script, I suggest creating the data in a temporary file and only at the end play the output of awk
in the file you need:
#!/bin/bash
# Faz o que precisa no arquivo temporário
date +"%Y-%m-%d %H:%M" >> /tmp/results
ps -ef | grep osh | wc -l >> /tmp/results
ps -ef | wc -l >> /tmp/results
# awk joga tudo no arquivo final
awk '{printf $0 " ";}' /tmp/results >> /home/dsadm/LogProcessos/psResults.csv
# apaga o arquivo temporário
rm /tmp/results
I did it because if you do awk ... arquivo > arquivo
it may overwrite the file and lose everything (and if do with >>
, it adds the content at the end of the file). Using different files is more guaranteed.
Since this is a CSV file, you can change the command to put a comma (or semicolon) instead of the space:
# vírgula
awk '{printf $0 ",";}' /tmp/results
# ponto-e-vírgula
awk '{printf $0 ";";}' /tmp/results
The problem is that it leaves a comma at the end (DATA,PROC1,PROC2,
). You can delete this by making a command a little more complex, to check if it is on the last line. Just use wc -l
to count the lines of the file and put it in a variable, which will be used by awk
:
# coloca vírgula entre os registros, exceto depois do último
awk -v last="$(wc -l < /tmp/results)" '{printf $0; if (NR != last) printf ","};' /tmp/results
With this, it only puts the comma if it is not the last record, resulting in DATA,PROC1,PROC2
Another alternative is to use sed
:
sed -e :a -e 'N;s/\n/ /;ta' psResults.csv
The option N
joins the current line with the next, and then s/\n/ /
replaces line break by space. ta
makes him make a loop for the label a
, that was set at the beginning (:a
). That is, it repeats this substitution process until the end of the file. In the end, all the lines are united in one, separated by space.
You can exchange comma, but you have the same problem of awk
(puts a comma at the end).
Your solution worked!
– Carlos Henrique Cardoso