Print line number and/or filename for selected entries #3449
-
|
Hello everyone. I’m very new to Let’s take the vitejs Convert this: "scripts": {
"preinstall": "npx only-allow pnpm",
"postinstall": "simple-git-hooks",
"format": "prettier --write --cache .",Into something like this: {
{ "line": 21, "key": "preinstall", "val": "npx only-allow pnpm" },
{ "line": 22, "key": "postinstall", "val": "simple-git-hooks" },
{ "line": 23, "key": "format", "val": "prettier --write --cache ." },For simplicity, you can choose any output format that makes the most sense. I know that Is there any way to do this? Any help would be greatly appreciated. Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Hi, sadly `input_line_number' does not give you the line number of individual value, it's the (last i think) line number of the whitepace-ish separated JSON values read. ex: $ (echo -e '{"a":\n123}'; echo '"hello"')
{"a":
123}
"hello"
$ (echo -e '{"a":\n123}'; echo '"hello"') | jq -c '{input: ., line: input_line_number}'
{"input":{"a":123},"line":2}
{"input":"hello","line":3}It is probably possible to use |
Beta Was this translation helpful? Give feedback.
-
|
Had to try using streamed input and it somewhat works except for the last entry that ends up one line too far, not really sure why but it seems line number if counted from "last" part of the value somehow, which probably also hints on that a package.json with lots of whitespace might cause weird line numbers also, $ jq --stream -n '[inputs | select((.[0] | .[0] == "scripts" and length > 1) and .[1]) as [[$_,$key], $script] | {$key, value: {$script, line: input_line_number}}] | from_entries' package.json
{
"preinstall": {
"script": "npx only-allow pnpm",
"line": 21
},
"postinstall": {
"script": "simple-git-hooks",
"line": 22
},
"format": {
"script": "prettier --write --cache .",
"line": 23
},
"lint": {
"script": "eslint --cache .",
"line": 24
},
.... |
Beta Was this translation helpful? Give feedback.
Had to try using streamed input and it somewhat works except for the last entry that ends up one line too far, not really sure why but it seems line number if counted from "last" part of the value somehow, which probably also hints on that a package.json with lots of whitespace might cause weird line numbers also,