If you have Splunk Enterprise, this command saves search results to the specified CSV file on the local search head in the
$SPLUNK_HOME/var/run/splunk/csv directory. Updates to
$SPLUNK_HOME/var/run/*.csv using the
outputcsv command are not replicated across the cluster.
outputcsv [append=<bool>] [create_empty=<bool>] [dispatch=<bool>] [usexml=<bool>] [singlefile=<bool>] [<filename>]
- Syntax: append=<bool>
- Description: If
append=true, the command attempts to append to an existing CSV file, if the file exists. If the CSV file does not exist, a file is created. If there is an existing file that has a CSV header already, the command only emits the fields that are referenced by that header. The command cannot append to .gz files.
- Syntax: create_empty=<bool>
- Description: If
create_empty=trueand there are no results, creates a 0 length file. When
create_empty=false, no file is created and if
append=false, the file is deleted if it previously existed.
- Syntax: dispatch=<bool>
- Description: If set to true, refers to a file in the job directory in
- Syntax: <filename>
- Description: Specify the name of a CSV file to write the search results. This file should be located in the
$SPLUNK_HOME/var/run/splunk/csvdirectory. Directory separators are not permitted in the filename. Filenames cannot contain spaces. If no filename is specified, the command rewrites the contents of each result as a CSV row into the
_xmlfield. Otherwise the command writes into a file. The
.csvfile extension is appended to the filename if the filename has no file extension.
- Syntax: singlefile=<bool>
- Description: If
singlefile=trueand the output spans multiple files, collapses the output into a single file.
- Syntax: usexml=<bool>
- Description: If there is no filename, specifies whether or not to encode the CSV output into XML. This option should not be used when invoking the
outputcsvfrom the UI.
There is no limit to the number of results that can be saved to the CSV file.
Internal fields added to the CSV file
When you run a search in Splunk Web, most internal fields are not included in the results.
outputcsv command does include many internal fields in the results in the CSV file.
To remove the internal fields, use the
fields command before the
outputcsv command in your search. The negative symbol ( - ) specifies to remove the fields. The underscore and asterisk ( _* ) specifies all internal fields. For example:
... | fields - _* | outputcsv MyTestCsvFile
To exclude specific internal fields from the output, you must specify each field separately. For example:
... | fields - _raw _indextime _sourcetype _subsecond _serial | outputcsv MyTestCsvFile
outputcsv command merges values in a multivalued field into single space-delimited value.
The command saves the
*.csv file on the local search head in the
$SPLUNK_HOME/var/run/splunk/ directory. The
*.csv files are not replicated on the other search heads.
1. Output search results to a CSV file
Output the search results to the 'mysearch.csv' file. The .csv file extension is automatically added to the file name if you don't specify the extension in the search.
... | outputcsv mysearch
2. Exclude internal fields from the output CSV file
You can exclude unwanted internal fields from the output CSV file. In this example, the fields to exclude are
index=_internal sourcetype="splunkd" | head 5 | fields - _indextime _sourcetype _subsecond _serial | outputcsv MyTestCsvfile
Have questions? Visit Splunk Answers and see what questions and answers the Splunk community has using the outputcsv command.
This documentation applies to the following versions of Splunk® Enterprise: 6.4.0, 6.4.1, 6.4.2, 6.4.3, 6.4.4, 6.4.5, 6.4.6, 6.4.7, 6.4.8, 6.4.9, 6.4.10, 6.5.0, 6.5.1, 6.5.1612 (Splunk Cloud only), 6.5.2, 6.5.3, 6.5.4, 6.5.5, 6.5.6, 6.5.7, 6.5.8, 6.5.9, 6.6.0, 6.6.1, 6.6.2, 6.6.3, 6.6.4, 6.6.5, 6.6.6, 6.6.7, 7.0.0, 7.0.1, 7.0.2, 7.0.3, 7.0.4