Barak
Barak

Reputation: 61

How do I write an output from bash script to the second line of the CSV file that contains headers?

I have a bash script that writes its output to the beginning of a CSV file. I need it to maintain the headers on the first line. I tried to use awk and sed but didn't succeed.

I got the main script which is used to make a SSH connection:

for n in $(cat list.txt)
do
ssh -t root@$n /etc/m_chkdsk_app.sh
done

list.txt contains servers names

 server1
 server2
 server3
 server4

and the run the following script on the remote computers

if [ -f   /lnxfiler/diskstatus/m_chkdsk.csv  ]
then
    printf "$(cat /proc/sys/kernel/hostname)" >> /lnxfiler/diskstatus/New_m_chkdsk.csv && printf "," >> /lnxfiler/diskstatus/New_m_chkdsk.csv && printf "$(date +%d-%m-%Y)" >> /lnxfiler/diskstatus/New_m_chkdsk.csv && df -h | grep /dev/mapper/rootvg-var | awk '{printf "," $2 "," $3 "," $5 "," $6 "\n"}' >> /lnxfiler/diskstatus/New_m_chkdsk.csv

    printf "$(cat /proc/sys/kernel/hostname)" >> /lnxfiler/diskstatus/New_m_chkdsk.csv && printf "," >> /lnxfiler/diskstatus/New_m_chkdsk.csv && printf "$(date +%d-%m-%Y)" >> /lnxfiler/diskstatus/New_m_chkdsk.csv && df -h | grep "/dev/mapper/rootvg-sap " | awk '{printf "," $2 "," $3 "," $5 "," $6 "\n"}' >> /lnxfiler/diskstatus/New_m_chkdsk.csv

    cat /lnxfiler/diskstatus/m_chkdsk.csv       >> /lnxfiler/diskstatus/New_m_chkdsk.csv
    mv /lnxfiler/diskstatus/New_m_chkdsk.csv   /lnxfiler/diskstatus/m_chkdsk.csv

else

    printf "$(cat /proc/sys/kernel/hostname)" >> /lnxfiler/diskstatus/m_chkdsk.csv && printf "," >> /lnxfiler/diskstatus/m_chkdsk.csv && printf "$(date +%d-%m-%Y)" >> /lnxfiler/diskstatus/m_chkdsk.csv && df -h | grep /dev/mapper/rootvg-var | awk '{printf "," $2 "," $3 "," $5 "," $6 "\n"}' >> /lnxfiler/diskstatus/m_chkdsk.csv

    printf "$(cat /proc/sys/kernel/hostname)" >> /lnxfiler/diskstatus/m_chkdsk.csv && printf "," >> /lnxfiler/diskstatus/m_chkdsk.csv && printf "$(date +%d-%m-%Y)" >> /lnxfiler/diskstatus/m_chkdsk.csv && df -h | grep "/dev/mapper/rootvg-sap " | awk '{printf "," $2 "," $3 "," $5 "," $6 "\n"}' >> /lnxfiler/diskstatus/m_chkdsk.csv

fi

exit

When I run the main script, I need all the output of the script to be added after the header.

 Server Name,Date,Disk Size,Used,Use%,Mounted on
 server1,08-09-2020,2.0G,363M,20%,/var
 server1,08-09-2020,15G,41M,1%,/usr/sap
 server1,08-09-2020,200G,237M,1%,/suse_manager
 server2,08-09-2020,2.0G,138M,8%,/var
 server2,08-09-2020,20G,6.6G,36%,/srv
 server2,08-09-2020,80G,6.7G,9%,/srv/NFS
 server3,08-09-2020,2.0G,363M,20%,/var
 server3,08-09-2020,15G,41M,1%,/usr/sap
 server4,08-09-2020,2.0G,138M,8%,/var
 server4,08-09-2020,20G,6.6G,36%,/srv
 server4,08-09-2020,80G,6.7G,9%,/srv/NFS

Upvotes: 0

Views: 683

Answers (2)

tripleee
tripleee

Reputation: 189936

Here's a quick refactoring.

Driver script; don't read lines with for:

head -n 1 result.csv >newresult.csv
while IFS= read -r host; do
do
    ssh -t "root@$host" /etc/m_chkdsk_app.sh </dev/null
done < list.txt >>newresult.csv
mv newresult.csv result.csv

Remote script:

df -h /dev/mapper/rootvg-var /dev/mapper/rootvg-sap | 
awk -v date=$(date +%d-%m-%Y) 'BEGIN { OFS="," }
  NR==FNR { host=$0; next }
  /\/dev/ { print host, date, $2, $3, $5, $6 }
  ' /proc/sys/kernel/hostname - |
tee /lnxfiler/diskstatus/m_chkdsk.csv

The original script had tremendous amounts of repetition but it's of course possible that I have overlooked some crucial difference between almost identical code snippets. That's actually one of the reasons to avoid repeating yourself. Selectively overwriting the old results on the remote server in slightly different fashion depending on whether the file already exists seemed entirely superfluous, so I took that out.

This assumes that you have old results in result.csv and that the first header line is so incredibly hard to get right that you have to copy it from the old file. It would probably be easier to just hard-code the script to write a new first line.

Depending on how robust you need this to be, maybe actually add set -e to the start of both scripts. If you don't absolutely have to store the results on the remote disk as well, that would cut out one of the main failure scenarios, and simpli|y the script still more.

Upvotes: 3

Paul Hodges
Paul Hodges

Reputation: 15428

If you have a file1.csv with data already in it, you can write all your new data to a tempfile.csv and then import it with an easy sed -

sed -i '1rtempfile.csv' file1.csv

This will read and add in the content of tempfile immediately after line 1.

Upvotes: 1

Related Questions