jq split jsonlines file into two [valid and nonvalid lines]

I frequently receive application logs from journald. The application itself logs with jsonlines, but sometimes I got invalid lines, if…:

  • something is written to stderr like Listening for transport dt_socket at address: 5045 or something like JProfiler > ....
  • systemd adds some logs itself like Started my.service - Service Description.
  • my log lines exceed the maximum journald line length, they are split up into multiple lines.

Long story short, I want to do some preprocessing of the shared log files where in a first step I only keep valid jsonlines and put all other lines into a dedicated second file for later review and processing.

My first attempt was to pipe the logfile through a bash script with running jq -e . >/dev/null 2>&1 <<< "$1" on each line. That turned out to be way too slow as my log files usually are about 2GB in size. So I would like to have a solution where I call jq only once and let it process all the loglines which would be way faster than calling jq once for each line.

I found the try catch syntax from jq but I don’t get a fully working solution with regards to the file containing the non jsonlines messages. Here are my best attempts so far:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cat input.log | jq -c -r -R '. as $line | try fromjson catch "($line)n" | stderr' > good.jsonl 2> bad.txt
</code>
<code>cat input.log | jq -c -r -R '. as $line | try fromjson catch "($line)n" | stderr' > good.jsonl 2> bad.txt </code>
cat input.log | jq -c -r -R '. as $line | try fromjson catch "($line)n" | stderr' > good.jsonl 2> bad.txt

This nicely keeps only good jsonlines, but it is horribly slow. Further,the “bad.txt” is all in one line and literally prints “n” as 2-char-string instead of newline character. Don’t know why.

I tried another formatting of that output string like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cat input.log | jq -c -r -R '. as $line | try fromjson catch ($line+="n" | $line | stderr) ' > good.jsonl 2> bad.txt
</code>
<code>cat input.log | jq -c -r -R '. as $line | try fromjson catch ($line+="n" | $line | stderr) ' > good.jsonl 2> bad.txt </code>
cat input.log | jq -c -r -R '. as $line | try fromjson catch ($line+="n" | $line | stderr) ' > good.jsonl 2> bad.txt

That runs much faster and produces proper jsonl good.jsonl but results in an unusable bad.txt like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>jq: error (at <stdin>:18126): Invalid path expression with result "Started my.service ...
</code>
<code>jq: error (at <stdin>:18126): Invalid path expression with result "Started my.service ... </code>
jq: error (at <stdin>:18126): Invalid path expression with result "Started my.service ...

where it literally prints only the first few characters of my broken message and not the entire line, hence I can’t reconstruct it.

The best solution I found so far is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cat input.log | jq -c -r -R '. as $line | if (fromjson) then . else $line | stderr end' > good.jsonl 2> bad.txt
</code>
<code>cat input.log | jq -c -r -R '. as $line | if (fromjson) then . else $line | stderr end' > good.jsonl 2> bad.txt </code>
cat input.log | jq -c -r -R '. as $line | if (fromjson) then . else $line | stderr end' > good.jsonl 2> bad.txt

This leaves my good.json with the JSONL as expected and produces a “bad.txt” like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>jq: error (at <stdin>:18126): Invalid numeric literal at line 1, column 8 (while parsing 'Started my.service - Service Description.')
jq: error (at <stdin>:18127): Invalid numeric literal at line 1, column 10 (while parsing 'Listening for transport dt_socket at address: 5045')
</code>
<code>jq: error (at <stdin>:18126): Invalid numeric literal at line 1, column 8 (while parsing 'Started my.service - Service Description.') jq: error (at <stdin>:18127): Invalid numeric literal at line 1, column 10 (while parsing 'Listening for transport dt_socket at address: 5045') </code>
jq: error (at <stdin>:18126): Invalid numeric literal at line 1, column 8 (while parsing 'Started my.service - Service Description.')
jq: error (at <stdin>:18127): Invalid numeric literal at line 1, column 10 (while parsing 'Listening for transport dt_socket at address: 5045')

If I find no better solution, I’ll go for the last one and post-edit the jq error message with some regex or similar to only keep the original line without the error details, but I wonder if I can somehow simply tell jq to directly output my original line without the error details? For me it looks like the catch clause has some issues with the jq string formatting options..

FYI: I am currently using jq 1.6 (installed on Ubuntu 22.04)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật