Fred J. Stephens wrote:
> How can I limit the width of column returned by a query?
> In a bash script I am returning an ID, and a text field from a table. 
> The text field can be any length, contain newlines, etc. The problems is 
> if a row of results contains a new line, the "grid" of rows and columns 
> is messed up. I can limit the length of the returned text with 
> substr(test,1,80), but that doesn't stop the newline appearing in the 
> output. Neither does .mode column, .width 3 80 (3 for the ID, 80 for the 
> text).
> I have tried dozens of combinations of piping the output to sed & tr to 
> try and remove any newlines, but if I do so, the output "grid" is all 
> crunched together with the next ID on the same row as the previous text.
> 
> What I need is for each row of the query results to end at 80 characters 
> or so and not wrap to another line - ie: one row per line.
> I hope I am making sense here.
> Any suggestions appreciated.

You can use the replace() function to strip the newlines from your 
strings. You could replace the newlines with empty strings or with a 
single space. The trick is entering the newlines. You can enter them 
directly in an sql script file.

select replace(some_column, '
',' ') from a_table;

Or you can use a cast and enter the expected newline characters for your 
system (i.e. Windows is 0d0a, *nix is 0a, etc).

select replace(some_column, cast(x'0d0a' as text), ' ') from table

Then the columns can be truncated using the substr() function as you 
suggest. The resulting strings should fit in your grid.

HTH
Dennis Cote
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to