0
0
Bash-scriptingConversionBeginner · 2 min read

Bash Script to Convert CSV to Text File

Use a Bash script with while IFS=, read -r to read CSV lines and echo to print fields separated by spaces, like: while IFS=, read -r col1 col2 col3; do echo "$col1 $col2 $col3"; done < input.csv > output.txt.
📋

Examples

Inputname,age,city Alice,30,New York Bob,25,Los Angeles
Outputname age city Alice 30 New York Bob 25 Los Angeles
Inputproduct,price,quantity Pen,1.5,10 Notebook,2.0,5
Outputproduct price quantity Pen 1.5 10 Notebook 2.0 5
Inputid,value 1,"Hello, world" 2,"Test, case"
Outputid value 1 "Hello world" 2 "Test case"
🧠

How to Think About It

To convert CSV to plain text, read each line of the CSV file, split it by commas, and print the fields separated by spaces. Handle each line as a set of columns, replacing commas with spaces to make it easier to read as plain text.
📐

Algorithm

1
Open the CSV file for reading.
2
For each line, split the line into columns using comma as the separator.
3
Print the columns separated by spaces instead of commas.
4
Repeat until all lines are processed.
5
Save the output to a text file.
💻

Code

bash
#!/bin/bash
input_file="input.csv"
output_file="output.txt"

while IFS=, read -r col1 col2 col3; do
  echo "$col1 $col2 $col3"
done < "$input_file" > "$output_file"

# Print output file content
echo "Converted text content:"
cat "$output_file"
Output
Converted text content: name age city Alice 30 New York Bob 25 Los Angeles
🔍

Dry Run

Let's trace the example CSV with header and two rows through the script.

1

Read first line

Reads line: 'name,age,city' splits into col1='name', col2='age', col3='city'

2

Print first line

Prints: 'name age city'

3

Read second line

Reads line: 'Alice,30,New York' splits into col1='Alice', col2='30', col3='New York'

4

Print second line

Prints: 'Alice 30 New York'

5

Read third line

Reads line: 'Bob,25,Los Angeles' splits into col1='Bob', col2='25', col3='Los Angeles'

6

Print third line

Prints: 'Bob 25 Los Angeles'

col1col2col3
nameagecity
Alice30New York
Bob25Los Angeles
💡

Why This Works

Step 1: Reading CSV lines

The script uses while IFS=, read -r to read each line and split it by commas into variables.

Step 2: Replacing commas with spaces

By printing the variables separated by spaces, the commas are effectively replaced with spaces in the output.

Step 3: Redirecting output

The output is redirected to a text file, creating a plain text version of the CSV content.

🔄

Alternative Approaches

Using tr command
bash
tr ',' ' ' < input.csv > output.txt
echo "Converted text content:"
cat output.txt
Simple and fast but does not handle quoted commas inside fields.
Using awk
bash
awk -F',' '{for(i=1;i<=NF;i++) printf "%s%s", $i, (i==NF ? "\n" : " ")}' input.csv > output.txt
echo "Converted text content:"
cat output.txt
More flexible and handles fields better but still limited for complex CSV.
Using csvtool (if installed)
bash
csvtool -t ',' -u ' ' cat input.csv > output.txt
echo "Converted text content:"
cat output.txt
Specialized CSV tool that handles CSV nuances but requires extra installation.

Complexity: O(n) time, O(1) space

Time Complexity

The script processes each line once, so time grows linearly with the number of lines.

Space Complexity

Uses constant extra space for variables; output file size depends on input size.

Which Approach is Fastest?

Using tr is fastest but less accurate; Bash loop is simple and reliable for small to medium files.

ApproachTimeSpaceBest For
Bash while loop with IFSO(n)O(1)Simple CSV with fixed columns
tr commandO(n)O(1)Very fast, simple CSV without quotes
awkO(n)O(1)More flexible CSV processing
csvtoolO(n)O(1)Complex CSV with quotes and escapes
💡
Use IFS=, read -r in Bash to split CSV lines easily by commas.
⚠️
Forgetting to set IFS=, causes the script to read whole lines without splitting fields.