Skip to content
Advertisement

read the first line of a text file with JQ

Trying to see how I can read the first line of a text file using jq

I have a text file with a bunch of ids (newfile.txt )

JavaScript

id like to be able to just read the first line with jq.

I tried doing this

JavaScript

But getting an error of

JavaScript

I’d like to be able to read line by line so that I can eventually run a look with that ID and be able to do stuff with it. Any ideas?

Advertisement

Answer

Use the -R argument (aka --raw-input) to tell jq that it’s receiving input as strings rather than JSON, and use input to read only a single item at a time. Thus:

JavaScript

…will output:

JavaScript

If you want to convert it to a number, that’s what tonumber is for:

JavaScript

…which will output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement