Score:0

How to convert terminal output to CSV format?

sl flag

I'm currently running two scripts (One using iostat and one using ssacli) to collect some hardware performance data. I run my scripts directing their output to a .txt file.

While this works for all intents and purposes, it requires a lot of manual, "fixing" of the data in an excel sheet to get it to a usable format for my needs.

How can I alter the output file to automate some of this? If I could for instance, replace all whitespace with commas. I can't seem to find any questions that talk about altering the terminal output in the way I need however.

Score:2
cn flag

Output of commands can be redirected and processed using so called pipes. A pipe is symbolized by the | sign and means "redirect the standard output of the program before the pipe symbol and feed it as standard input to the program after the pipe". This allows to process output of a command using text processing tools before redirecting the changed content to a file.

Replacing spaces by comma's, for example, could be achieved with

yourscript | tr ' ' ',' > output_textfile.txt

Multiple pipes can be set after each other, allowing for some pretty complex on-the-fly text processing and formatting. See for example a list of standard unix text processing tools.

cVos avatar
sl flag
Thanks so much, this was exactly what I was looking for.
Score:0
ir flag

This is more a general bash scripting question than an Ubuntu related one. You may find more useful stuff in StackOverflow or here for instance.

To replace all spaces in yourfilename.txt with commas you can use this:

#!/bin/bash
    
FILE="yourfilename.txt"
    
    while read -r line; do
        echo "${line// /,}"
    done <$FILE > newfilename.csv
I sit in a Tesla and translated this thread with Ai:

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.