Fish Shell:
Working with CSV
How to:
Fish Shell, by itself, doesn’t have built-in functions specifically designed for CSV manipulation. However, you can leverage Unix utilities like awk, sed, and cut for basic operations or use specialized tools like csvkit for more advanced tasks.
Reading a CSV file and printing the first column:
Using cut to extract the first column:
cut -d ',' -f1 data.csvSample output:
Name
Alice
BobFiltering CSV rows based on column value:
Using awk to find rows where the second column matches “42”:
awk -F, '$2 == "42" { print $0 }' data.csvSample output:
Bob,42,LondonModifying a CSV file (e.g., adding a column):
Using awk to add a column with a static value “NewColumn”:
awk -F, 'BEGIN {OFS=","} {print $0,"NewColumn"}' data.csv > modified.csvSample output in modified.csv:
Name,Age,City,NewColumn
Alice,30,New York,NewColumn
Bob,42,London,NewColumnUsing csvkit for more advanced operations:
First, ensure you have csvkit installed. If not, install it using pip: pip install csvkit.
Converting a CSV file to JSON:
csvjson data.csv > data.jsonSample data.json output:
[{"Name":"Alice","Age":"30","City":"New York"},{"Name":"Bob","Age":"42","City":"London"}]Filtering with csvkit’s csvgrep:
csvgrep -c 2 -m 42 data.csvThis command replicates the filtering task but using csvkit, targeting column 2 for the value “42”.
In conclusion, while Fish Shell itself might not offer direct CSV manipulation capabilities, its seamless integration with Unix utilities and the availability of tools like csvkit provide powerful options for working with CSV files.