-1

I'm decomposing a single line multiple times, recomposing it after each step, but each command adds a new line to the output.

Basically, these are the commands:

h
s#(^.*?)(\[\{.*$)#\1#p
g
s#(^.*?)(\[\{.*?\}\])(.*)$#echo \2 | jq --sort-keys --compact-output#ep
g
s#(^.*?\}\])(.*)$#\2#p
z

But the original line results in three lines after sed, because each /p adds a newline after the content: How can I avoid that? One line from input should result in one line in the ouput.

Instead of /p, I also tried writing the s commands result to file with /w filename flag, then read it with r filename command, but the file content is added straight to the output, giving the same result.

And /p is because I tried adding the -n command line parameter to sed.

To add a bit of context: I'm parsing logs HTTP POST requests, which are made of a timestamp, the request URI, the request JSON body and the request headers and I'm trying to use jq to uniformly order that JSON properties.

2
  • 1
    You may want to post the actual JSON document that you're working with and an explanation of the changes that you want to make to this document. Using sed is not adequate for parsing or modifying JSON.
    – Kusalananda
    Jul 28, 2021 at 14:44
  • @Kusalananda Agreed, but I think that makes for a different question (and I'll post another one); here I'm asking whether there's a way to tell sed not to automatically add a newline when writing output - that is instead what is being done, as stated in the docs.
    – watery
    Jul 28, 2021 at 15:19

2 Answers 2

0

If I correctly understood your question, you can use tr -d '\n' to remove newlines, including the ones introduced by sed.

3
  • Thanks. I hoped there was a way to tell sed not to output the newline tough, to save an additional parsing / processing step of the whole file.
    – watery
    Jul 28, 2021 at 10:47
  • Ah, forgot to mention: I need to merge (remove newlines) from groups of three rows, not join them all. One row from the input file is split into three (by the three s steps), I need to join them back in one.
    – watery
    Jul 28, 2021 at 13:12
  • 1
    @watery Please edit your question to include these details. Also, please provide a (possibly anonymized) example of the input to be processed, along with the desired result of your sed operations.
    – AdminBee
    Jul 28, 2021 at 14:41
0

I managed to obtain the result I wanted, with the following script:

h
# Extract the first part of the input line and store it as-is
s#(^.*?)(\[\{.*$)#\1#
H
g
# (1)
s#$(\n|.)*\'##m
# Extract the second part of the input line and process it with an external command
s#(^.*?)(\[\{.*?\}\])(.*)$#echo \2 | jq --sort-keys --compact-output#e
H
g
# (1)
s#$(\n|.)*\'##m
# Extract the third (and final) part of the input line and store it as-is
s#(^.*?\}\])(.*)$#\2#m
H
g
# (2) 
s#^.*$\n##m
# (3)
s#\n##mg

I basically keep the original input in the hold space, by initially copying it (with h), then accumulating partial processing results appending them to the hold space (with H). Thus, when I replace the pattern space with the hold space (with g) I need to remove those extra components (with command (1)).

At the end of the processing, I remove the first line - that is the original input line - (with command (2)) and remove all the newlines added by previous usage of H commands (with command (3)), effectively recomposing a single line before printing the pattern space to output.

Note: I soon discovered that the shell command invocation is a real blocker, it slows down processing a single file (the processing does several steps other thatn this with sed) from a couple of minutes to too much to wait.

2
  • What version of sed are you using? Bcoz the non-greedy regex .*? is not available in GNU sed. Only superseded ssed maybe has this feature.
    – guest_7
    Jul 31, 2021 at 14:30
  • @guest_7 Version 4.2.1 from GnuWin32 (I'm on Windows).
    – watery
    Jul 31, 2021 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.