I am trying to work with a large text file to place data that is bounded with parenthesis onto a single line. Currently it is broken across multiple lines...

What it looks like:


What I need to have it look like:

(123456 'abcdef' 'a1b2c3d4''1234567890' 0)
(789012 'zyxwv''987gha871''987654828' 0)

Trying to knock this out using a sed one-liner, but not having much luck. Any thoughts?


  • 2
    sed 'H;/(/h;/)/!d;x;s/\n/ /g' works with your input sample... it's unclear whether the file may contain lines outside those parens (and if so what's the expected output) – don_crissti Sep 5 '17 at 18:24
  • If you are able to use awk, I have a simple solution for you – RomanPerekhrest Sep 5 '17 at 18:42

awk solution:

awk '{ printf "%s%s",$0,($0~/\)$/)? ORS:FS }' file

The output:

(123456 'abcdef' 'a1b2c3d4' '1234567890' 0)
(789012 'zyxwv' '987gha871' '987654828' 0)

A Perl solution:

perl -nle 'if (/\(/../\)/) {push @a,$_;if (m/\)/) {print "@a";@a=()}}' file

This uses the range operator ("..") to capture in a any everything seen between (and including) an open parentheses and a closing one. Once a closing parenthesis is sensed, the array contents are printed and the array cleared for the next cycle.

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.