Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq remove spaces after first

Tags:

regex

jq

Seemed simple, but not so far. Tried lots of things. Best I've got:

echo "low quality      not gonna apologize" | jq -r 'gsub("[\\s+]"; " "; "g")'

parse error: Invalid numeric literal at line 1, column 4

Goal is to have 1 space replace any occurrence of multiple whitespace of any kind. Note that I removed tabs and newlines already from this stream. This is bash shell. I don't get this error in the context of the larger application I'm building either, where the code is simply and quietly not changing the multiple spaces into a single space for IDK why.

like image 311
Geoffrey Anderson Avatar asked Oct 26 '25 06:10

Geoffrey Anderson


1 Answers

The right way with jq:

echo "low quality      not gonna apologize" | jq -Rr 'gsub("\\s+";" ";"g")'
  • -R - raw input; each line of text is passed to the filter as a string

The output:

low quality not gonna apologize
like image 68
RomanPerekhrest Avatar answered Oct 28 '25 20:10

RomanPerekhrest