Skip to content

Commit

Permalink
Merging pull request of @JKrag (thanks!), with some minor changes, ma…
Browse files Browse the repository at this point in the history
…inly changing the output delimiter letter and adding some comments
  • Loading branch information
JKrag authored and harelba committed Jan 15, 2014
1 parent 40e9509 commit 8ec0cb9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .qrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
# Available options:
# * delimiter - escaped string (e.g. use \t for tab or \x20 for space)
# * outputdelimiter - escaped string (e.g. use \t for tab or \x20 for space)
# * gzipped - boolean True or False
# * beautify - boolean True or False
# * header_skip - integer number of lines to skip at the beginning of the file
Expand All @@ -16,6 +17,7 @@

[options]
#delimiter: \t
#output_delimiter: \t
#gzipped: False
#beautify: True
#header_skip: 0
Expand Down
2 changes: 2 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ q can also get some runtime flags (Linux style, before the parameter). The follo
* `-z` - Means that the file is gzipped. This is detected automatically if the file extension if .gz, but can be useful when reading gzipped data from stdin (since there is no content based detection for gzip).
* `-H <N>` - Tells q to skip N header lines in the beginning of the file - Used naturally for skipping a header line. This can possibly be detected automatically in the future.
* `-d` - Column/field delimiter. If it exists, then splitting lines will be done using this delimiter. If not provided, **any whitespace** will be used as a delimiter.
* `-D` - Column/field delimiter for output. If it exists, then the output will use this delimiter instead of the one used in input. Defaults to input delimiter if provided by `-d`, or space if not.
* `-b` - Beautify the output. If this flag exists, output will be aligned to the largest actual value of each column. **NOTE:** Use this only if needed, since it is slower and more CPU intensive.
* `-t` - Shorthand flag for a tab delimiter, one header line format (Same as `-d $'\t' -H 1` - The $ notation is required so Linux would escape the tab...)
* `-f <F>` - Output-formatting option. If you don't like the output formatting of a specific column, you can use python formatting in order to change the output format for that column. See below for details
Expand Down Expand Up @@ -151,3 +152,4 @@ This tool has been designed with general Linux/Unix design principles in mind. I
Any feedback/suggestions/complaints regarding this tool would be much appreciated. Contributions are most welcome as well, of course.

Harel Ben-Attia, [email protected], @harelba on Twitter

17 changes: 14 additions & 3 deletions q
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def get_option_with_default(p,option_type,option,default):
default_beautify = get_option_with_default(p,'boolean','beautify',False)
default_gzipped = get_option_with_default(p,'boolean','gzipped',False)
default_delimiter = get_option_with_default(p,'escaped_string','delimiter',None)
default_output_delimiter = get_option_with_default(p,'escaped_string','output_delimiter',None)
default_header_skip = get_option_with_default(p,'int','header_skip',0)
default_formatting = get_option_with_default(p,'string','formatting',None)
default_encoding = get_option_with_default(p,'string','encoding','UTF-8')
Expand Down Expand Up @@ -88,6 +89,8 @@ parser.add_option("-z","--gzipped",dest="gzipped",default=default_gzipped,action
help="Data is gzipped. Useful for reading from stdin. For files, .gz means automatic gunzipping")
parser.add_option("-d","--delimiter",dest="delimiter",default=default_delimiter,
help="Field delimiter. If none specified, then standard whitespace is used as a delimiter")
parser.add_option("-D","--output-delimiter",dest="output_delimiter",default=default_output_delimiter,
help="Field delimiter for output. If none specified, then the -d delimiter is used if present, or space if no delimiter is specified")
parser.add_option("-t","--tab-delimited-with-header",dest="tab_delimited_with_header",default=False,action="store_true",
help="Same as -d <tab> -H 1. Just a shorthand for handling standard tab delimited file with one header line at the beginning of the file")
parser.add_option("-H","--header-skip",dest="header_skip",default=default_header_skip,
Expand Down Expand Up @@ -506,10 +509,18 @@ m = sql_object.execute_and_fetch(db)
if options.beautify:
max_lengths = determine_max_col_lengths(m)

if options.delimiter:
output_delimiter = options.delimiter
if options.output_delimiter:
# If output delimiter is specified, then we use it
output_delimiter = options.output_delimiter
else:
output_delimiter = " "
# Otherwise,
if options.delimiter:
# if an input delimiter is specified, then we use it as the output as well
output_delimiter = options.delimiter
else:
# if no input delimiter is specified, then we use space as the default
# (since no input delimiter means any whitespace)
output_delimiter = " "

if options.formatting:
formatting_dict = dict([(x.split("=")[0],x.split("=")[1]) for x in options.formatting.split(",")])
Expand Down

0 comments on commit 8ec0cb9

Please sign in to comment.