In this step, we will explore how to use the column
command with various file formats and delimiters. This will help you understand the versatility of the column
utility and how it can be applied to different types of data.
Working with CSV Files
CSV (Comma-Separated Values) files are a common format for storing tabular data. Let's create a more complex CSV file and use the column
command to format it.
First, create a new CSV file:
cd ~/project
echo -e "Name,Age,Occupation,City\nAlex,28,Engineer,Boston\nSamantha,35,Teacher,Chicago\nMohamed,42,Doctor,New York\nLin,31,Artist,San Francisco" > employees.csv
Let's examine the content of this file:
cat employees.csv
You should see:
Name,Age,Occupation,City
Alex,28,Engineer,Boston
Samantha,35,Teacher,Chicago
Mohamed,42,Doctor,New York
Lin,31,Artist,San Francisco
Now, let's use the column
command to format this CSV file:
column -t -s ',' employees.csv
The output should look like this:
Name Age Occupation City
Alex 28 Engineer Boston
Samantha 35 Teacher Chicago
Mohamed 42 Doctor New York
Lin 31 Artist San Francisco
Notice how the column
command has neatly arranged the data in aligned columns, making it much easier to read.
Working with TSV Files
TSV (Tab-Separated Values) is another common format for tabular data. Let's create a TSV file and format it using the column
command.
Create a TSV file:
echo -e "Product\tPrice\tCategory\nLaptop\t999.99\tElectronics\nBook\t12.50\tMedia\nChair\t149.50\tFurniture" > products.tsv
Let's look at the content:
cat products.tsv
You should see:
Product Price Category
Laptop 999.99 Electronics
Book 12.50 Media
Chair 149.50 Furniture
Now, format it using the column
command. Since tabs are the default delimiter for the column
command, we don't need to specify a delimiter:
column -t products.tsv
The output should look like:
Product Price Category
Laptop 999.99 Electronics
Book 12.50 Media
Chair 149.50 Furniture
Using Our Script with Different Files
Now, let's use our columnize.sh
script with these different files:
For the CSV file:
~/project/columnize.sh employees.csv ,
For the TSV file:
~/project/columnize.sh products.tsv $'\t'
Note: In the second command, we're using $'\t'
to represent a tab character. This is a special syntax in bash that allows us to include special characters like tabs.
Both commands should produce nicely formatted output, demonstrating the flexibility of our script with different file formats and delimiters.
This step has shown how the column
command and our script can be used to format various types of tabular data, making them more readable and easier to analyze.