I was curious if there is a clean way to add line numbers to jq's output while still retaining the coloring of the output. I have tried piping from jq to cat -n but unfortunately this removes the coloring which helps a lot when traversing the JSON tree.
Specify the jq -C option explicitly.
Related
I am trying to run a jq query on a windows machine and it extracts values from output on a separate line
jq -r .Accounts[].Id
Output
204359864429
224271824096
282276286062
210394168456
090161402717
How do I run the jq query so that it combines the output on a single line separated by space
This is what I need-
204359864429 224271824096 282276286062 210394168456 090161402717
Any help would be appreciated.
The usual way would be to use the #csv or #tsv operators to convert the result in the CSV or tab-delimited format. These operators need the result to be contained in an array. For your case also to have a single space delimit, we can do a simple join(" ") operation
jq -r '[.Accounts[].Id]|join(" ")'
You can use the #sh formatter:
jq -r ".Accounts[].Id | #sh"
From the jq docs:
The input is escaped suitable for use in a command-line for a POSIX shell. If the input is an array, the output will be a series of space-separated strings.
Reference:
https://stedolan.github.io/jq/manual/#Basicfilters
At first I thought the join() solution above did not work. Then I realized that I was "overfeeding" the join() filter, causing it to fail because I was providing more than a simple array as input. I had concatenated several filters with , and failed to limit the scope of my join().
Did not work:
jq -r \
'.ansible_facts |
.ansible_hostname,
.ansible_all_ipv4_addresses | join(" "),
.ansible_local."aws-public-ipv4".publicIP'
This gave the error,
jq: error (at <stdin>:0): Cannot iterate over string ("hostone")
because jq was attempting to "consume" not only ansible_all_ipv4_addresses but also the output of the preceding ansible_hostname filter (I am not certain why this is or whether it was even intended by the author of jq).
Does work:
jq -r \
'.ansible_facts |
.ansible_hostname,
(.ansible_all_ipv4_addresses | join(" ")),
.ansible_local."aws-public-ipv4".publicIP'
Here, I restrict join() to .ansible_all_ipv4_addresses only (ansible_all_ipv4_addresses is an array of IP addresses I wish to translate into a single, space-separated string).
P.S.: I found that the #sh filter produces space-separated output as desired, but in addition delimits each output item in single quotes.
P.P.S.:
Here was my workaround, until I discovered that join() works just the same as it when used properly (see above):
jq -r '.Accounts[].Id | #tsv | sub("\t";" ";"g")'
Explanation: the #tsv filter produces Tab Separated Values, then the sub() filter substitutes tabs with spaces, globally.
I am stuck on a homework question. The question asks to display the lines, with grep and I can't use -w option, that contain no duplicate vowels.
My teacher said to find the grep command that could display two or more 'a's in a line which would, I think, be grep 'a.*a' file and then find the grep command that would display two or more 'u's which, I think, would be grep 'u.*u' file, combine them and then I should be able to get it. But I don't know how I would combine the grep commands.
You can combine different regular expressions with |:
grep 'a.*a|e.*e|i.*i|o.*o|u.*u' file
Its a simple question but giving too much trouble to workaround.
All solutions mentioned works with ksh99. But unfortunately i use ksh88 and i am unable to get substring from a string.
I am trying to get year part of the string. but i am getting an error. The cut syntax seems fine. also the assignment to the variable.
cut: The list arguments following the c option are not correct.
Here is the statement used.
typeset -i dt_year=`echo 201610118 | cut -c1-4`
I would in ksh88 separate your line in:
typeset -i dt_year=0
dt_year=`echo "201610118" | cut -c1-4`
You can also try to leave out the cut -c-4
And check with alias if cut is aliased.
I need a unix command that would search multiple patterns (basically an AND), however those patterns need not be on the same line (otherwise I could use grep AND command). For e.g. suppose I have a file like following:
This is first line.
This is second line.
This is last line.
If I search for words 'first' and 'last', above file should be included in the result.
Try this question, seems to be the same as yours with plenty of solutions: How to find patterns across multiple lines using grep?
I think instead of AND you actually mean OR:
grep 'first\|last' file.txt
Results:
This is first line.
This is last line.
If you have a large number of patterns, add them to a file; for example if patterns.txt contains:
first
last
Run:
grep -f patterns.txt file.txt
I want to pipe the output of a command to something able to color the occurrences of a specific word.
Example: echo "ABC DEF GHI" | magic_color_thing("DEF") should print out ABC DEF GHI with DEF colored.
I want to do it with ZSH and I want to preserve all the lines as well as the carriage returns.
Thank you in advance for any help!
If you have (a recent version of) GNU grep, use its --color option. To have it print non-matching lines as well, use a pattern that matches the empty string.
… | grep --color -E '|DEF'
If you want to do it entirely within zsh, make it iterate over the lines, surrounding DEF with color codes.
autoload colors; colors
while IFS= read -r line; do
print -r -- "${line//DEF/$fg[red]DEF$fg[default]}"
done
See also How to have tail -f show colored output, and a few other questions tagged color.
Does
echo "...... DEF....." | grep --color "DEF"
do the job for you?
It would help if you said more about the kind of data you were piping in.
(And also whether lines without matches are important or not)