Quick Start - the simple way
In short, this is what you need to know for starting:
- All commands start with a plus sign (+), a minus sign (-), or an percent sign (%).
- In all commands, the "+" (plus) command prefix stands for reading or processing the input table, and the "-" (minus) prefix will modify the output table. The "%" (percent) prefix has other purposes.
- Mostly, a command will contain a column name or a list of column names, and/or an "=" (equal sign) followed by an expression or by a list of expressions.
- Furthermore, the first parameter which does not look like a command denotes the input file, while the last one denotes the output file. (You can override this by declaring the input file and the output file explicitly through "+in=filepath" and/or "-out=filepath".)
- The CSV data fields can be accessed with their column names in square brackets, like this: [My Field], with their index like [3], or in "Excel style" like [A]. Generally, the contents of the fields in the first data row are interpreted as the column names.
- Column names can contain spaces, so when accessing them on the command line better enclose them between doublequotes to prevent tearing the commandline parameter apart.
CSV Table Transformation on the CLI
Changing the CSV table format
The following command changes the CSV field delimiter to a different character, from the comma (,) to a semicolon (;).
This is the raw contents of the CSV table "people_comma.csv":
Now we will enter this command:
And this will be the resulting CSV table "people_semicolon.csv":
Please see also that, according to the CSV standard, all fields that contain the actual delimiter character will automatically be enclosed with doublequote characters.
Rearranging CSV data fields order
The CSV data fields can be accessed with their field names in square brackets, like this: [My Field] , or with their index like [3].
Name | Age | Eye Color | Hair color |
---|---|---|---|
Anna | 27 | brown | black |
Harald | 59 | grey | blonde; now grey |
Peter | 38 | Left: brown, right: blue | red |
Enumerating the output columns with the "-columns" command will rearrange the columns as defined. Columns not named in this command will be omitted for output.
So, rearranging will be done like this:
Name | Eye Color | Age |
---|---|---|
Anna | brown | 27 |
Harald | grey | 59 |
Peter | Left: brown, right: blue | 38 |
Recalculating of a CSV data field
This is the table "Articles.csv", a collection of some clothing for sale.
ArtNo | Article Name | Price | Size |
---|---|---|---|
348593485 | Long Sleeve Business Shirt | 65.00 | XX |
585048534 | Trousers | 70.00 | XL |
459845414 | Business Jacket | 85.00 | XL |
524525257 | Fine Leather Shoes | 235.00 | EU46 |
So now the shop will increase all prices by 20 percent.
The new prices shall become inserted with 2 decimal places:
ArtNo | Article Name | Price | Size |
---|---|---|---|
348593485 | Long Sleeve Business Shirt | 78.00 | XXL |
585048534 | Trousers | 84.00 | XL |
459845414 | Business Jacket | 102.00 | XL |
524525257 | Fine Leather Shoes | 282.00 | EU46 |
Using a "job" file
(missing example)
This works line by line. At first, the header line is read (if it exists), and all field names are detected.
After this, the table rows are read one after each other, and each line will be processed command by command.
The fields can be accessed with their field names in square brackes like this:
Additional fields from other tables can be merged, and the whole table can be sliced (reformatted) into newly arranged derived rows.
At last the table will be saved under a new name.
CSVfox is also suitable for automation, like usage in batch files or PowerShell scripts, so it is supposed to run on schedule by an OS, such as Cron or Task Scheduler.
More complex commands or configurations can be combined as a " job " file, for applying and re-use in various situations.
Some more Examples
- csvfox infile.csv +e=utf-8 +d=";" -e=Windows-1252 -d=pipe outfile.csv
- This command changes the format of the CSV file.
It readsinfile.csv
, assuming UTF-8 encoding and semicolon as delimiter. Then it writes the data tooutfile.csv
, while changing delimiters to "|" and the encoding to ANSI (Windows).
See also ±encoding and ±delimiter. - csvfox prices.csv +e=windows-1252 -set[Price]="{([Price]+20%),2}" -e=utf-8 newprices.csv
- This recalculates a numeric field.
It reads the Windows-formatted CSV file "prices.csv", increases the existing CSV columnPrice by 20 percent, and formats the result with decimal comma and two decimal places. Then writes all data out to "newprices.csv" using UTF-8.
See also Calculating with Numeric CSV Fields. - csvfox newprices.csv -add["Special Offer Text"]="Just for you: [Price]!" specialoffers.csv
- This command appends a new column and fills it with text.
It appends the new columnSpecial Offer Text to the CSV table and inserts some text including the contents ofPrice there.
See also Expressions and their Resolving. - csvfox cities.csv %merge[Name]=[Location]@attractions.csv cityattractions.csv
- This example combines two files.
It enriches the data in "cities.csv" with all fields of the attractions listed in "attractions.csv", where the columnLocations in "attractions.csv" matches the fieldName in "cities.csv" . The resulting data will be written to "cityattractions.csv".
See also %merge.