DEV Community

Eloy Pérez
Eloy Pérez

Posted on • Updated on

Format CSV in your terminal using awk

It's pretty common to have a CSV file that you want to have a look at and you are forced to either open it in a software that support reading such files like Libreoffice Calc or you open it as a plain text file in which case you are not able to read the content easily because it's not pretty printed.

However, if you already have a terminal open, you can easily format the file right in your terminal without the need for extra software. Say we have the following CSV file:

Type, Primary Text, Name, Description, Owner
"A type", "Lorem ipsum dolor sit amet consectetur adipiscing elit", "A name", "This requirement defines something", "John Snow"
"Something else", "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua", "Another name", "This is THE requirement", "Eloy Perez"
"Important stuff", "consectetur adipiscing elit", "And another one", "It cannot be", "Luke"
Enter fullscreen mode Exit fullscreen mode

And you want to pretty print it right in your terminal, you can use awk for that.

awk -F',' '{ printf "%-030s %-70s %-30s %-40s %-30s\n", $1, $2, $3, $4, $5 }' file.csv
Type                            Primary Text                                                           Name                           Description                              Owner
"A type"                        "Lorem ipsum dolor sit amet consectetur adipiscing elit"               "A name"                       "This requirement defines something"     "John Snow"
"Something else"                "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"    "Another name"                 "This is THE requirement"                "Eloy Perez"
"Important stuff"               "consectetur adipiscing elit"                                          "And another one"              "It cannot be"                           "Luke"
Enter fullscreen mode Exit fullscreen mode

By default, awk works with text that uses whitespace as separator -F',' specifies what separator to use. In this case we are working with Comma-Separated Values, so we will use a ,

After that, we configure the output for each line of the file. We configure each column with the width of the content it will contain, for example %-30s means that it will have a string (%s) aligned to the left (%-s character) and it will be 30 characters wide (%-30s)

Then we specify what values are going to be printed $1, $2, $3, $4, $5 which are the return values of splitting the original string with the separator ,.

If we wanted to only print certain columns, we just have to not add them to the awk program. Let's say we only want to show Type and Name:

awk -F',' '{ printf "%-30s %-30s\n", $1, $3 }' file.csv
Type                            Name
"A type"                        "A name"
"Something else"                "Another name"
"Important stuff"               "And another one"
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
drhyde profile image
David Cantrell

Be careful though, if one of the CSV fields has an embedded comma like "Another, and another, and so on ad infinitum" you're going to get into trouble!