Format CSV in your terminal using awk
Tags: csv, terminal, unix, productivity
Published: 2022-11-28 12:02:00

---
title: "Format CSV in your terminal using awk"
published: true
description: "Format CSV in your terminal using awk"
tags:
    - csv
    - terminal
    - unix
    - productivity
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
published_at: 2022-11-28 12:02 +0000
---


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"
```

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

```bash
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"
```

By default, `awk` works with text that uses whitespace as separator `-F','` specifies what separator to use. In this case we are working with **C**omma-**S**eparated **V**alues, 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`:

```bash
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"
```