Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to truncate field data

Tags:

jq

I have some fields that are really long, but I just want to see the beginning of them. Is there a way to truncate a field to only the first X characters?

like image 802
David Lang Avatar asked Dec 10 '15 22:12

David Lang


People also ask

How do I truncate a field in Excel?

Insert the TRUNC formula. In the first cell of the column, you want to populate with the truncated number, type an "=" sign to indicate you're using a formula. Then type TRUNC. Use an open parenthesis to add the operators of the formula. The first number you enter is the number you want to truncate.

How do you truncate a field value in SQL?

What is the Truncate Command in SQL? We use the Truncate command to delete the data stored in the table. The working of truncate command is similar to the working of Delete command without a where clause.

Can you truncate text in Excel?

The formula is "=DIRECTION(Cell Name, Number of characters to display)" without the quotation marks. For example: =LEFT(A3, 6) displays the first six characters in cell A3. If the text in A3 says "Cats are better", the truncated text will read "Cats a" in your selected cell.


2 Answers

If you mean by trimming long strings, sure. Figure out which strings to trim then trim them.

e.g., trimming a string to the first 10 characters

$ echo '"12345678901234567890"' | jq '.[0:10]'

Read, take the first 0-10 characters of the string.

like image 60
Jeff Mercado Avatar answered Oct 08 '22 18:10

Jeff Mercado


If you want to recursively trim all strings:

.. |= (if type == "string" then .[0:2] else . end)

For example, if the input is:

{"a": "aaaaaaaaaaaaaaaaaaaaaaaaaaaa", 
 "b": "bbbbbbbbbbbbbbbbbbbbbbbbbb",
 "c": ["ddddddddddddddd"]
}

the output (compacted) would be:

{"a":"aa","b":"bb","c":["dd"]}
like image 26
peak Avatar answered Oct 08 '22 18:10

peak