Score:0

How to either export txt file to csv, or append to a csv file with a new column but same row - Bash

us flag

I am working on an inventory script to be used which will collect computer information across many devices and am looking for the best way to end up with a csv file with the information

Currently, I have this:

touch ZI_IT_Inventory_output.txt
touch ZI_IT_Inventory_output.csv




user_whoami=$(whoami)
user_lscpu=$(lscpu | sed -nr '/Model name/ s/.*:\s*(.*) @ .*/\1/p' )
user_lshw=$(lshw -C display |grep 'product' | cut -f 2 -d":" | awk '{$1=$1}1' )
user_how_many_gpu=$(lspci | grep VGA | wc -l)
user_how_many_cpu=$(lscpu | sed -nr '/Model name/ s/.*:\s*(.*) @ .*/\1/p' | wc -l)
user_how_much_ram=$(lshw -c memory |grep 'size' | cut -d':' -f2- | cut -d' ' -f2)



echo $user_whoami>>ZI_IT_Inventory_output.txt
echo $user_lscpu>>ZI_IT_Inventory_output.txt
echo $user_lshw>>ZI_IT_Inventory_output.txt
echo $user_how_many_gpu>>ZI_IT_Inventory_output.txt
echo $user_how_many_cpu>>ZI_IT_Inventory_output.txt
echo $user_how_much_ram>>ZI_IT_Inventory_output.txt

Which creates a txt file of this:

ziadmin
Intel(R) Core(TM) i7-6700 CPU
HD Graphics 530
1
1
8GiB

Which is the best way I could output this as a csv file where all the data is on the same row but different columns

24601 avatar
in flag
https://stackoverflow.com/questions/40175868/how-to-create-csv-file-using-shell-script
Score:0
it flag

Simply replace your multiple echo commands:

echo "$user_whoami,$user_lscpu,$user_lshw,$user_how_many_gpu,$user_how_many_cpu, $user_how_much_ram" >>ZI_IT_Inventory_output.txt
Gliotto avatar
us flag
Thank you! Just adding, >> as a csv instead of txt and it worked
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.