-3

If two patterns are found then convert in into single line from start to end pattern.

Example-

cat test.txt

qwe rty

uio {asd

fgh jkl

zxc} vbn

mqw rty

Now desired o/p should be

qwe rty

uio {asd fgh jkl zxc}

vbn

mqw rty

Here First pattern is "{" and second pattern is "}"

Thanks.

HatLess
  • 10,622
  • 5
  • 14
  • 32

4 Answers4

0

Solution in TXR.

With Syntax Highlighting (Vim)

$ txr data.txr data
qwe rty
uio {asd fgh jkl zxc}
vbn
mqw rty

data is:

qwe rty
uio {asd
fgh jkl
zxc} vbn
mqw rty

data.txr is:

@(repeat)
@  (cases)
@pre{@post
@(freeform " ")
@rest} @; <-- space after @rest} here.
@    (do (put-line `@pre{@post @rest}`))
@  (or)
@nomatch
@    (do (put-line nomatch))
@  (end)
@(end)

@pre@{post means we match a line which contains a { character. Everything before the { character goes into the pre variable; everything after goes to post.

Then freeform " " means that for the purposes of subsequent matching, the input lines are considered to be one single line, in which they are joined by spaces. Inside this one giant line we match @rest} : a prefix portion which goes into the variable rest up to a closing brace, and we match the brace also and a following space. If all this material matches, we output it as one line.

If there is no match such as the above, the second case matches a line, capturing it into nomatch and that is put out as-is.

The surrounding repeat then marches down past the matched lines to try matching again.

Kaz
  • 55,781
  • 9
  • 100
  • 149
0

This might work for you (GNU sed):

sed '/{/{:a;N;/}/!ba;s/\n\+/ /g;s/}\s*/&\n/}' file

Gather up lines starting with { and ending with }.

Replace each newline (or multiples of newlines) with a space.

Insert a newline following the } and remove any possible trailing white space.

potong
  • 55,640
  • 6
  • 51
  • 83
0

I would harness GNU AWK for this task following way, let file.txt content be

qwe rty
uio {asd
fgh jkl
zxc} vbn
mqw rty

then

awk '/{/{ORS=" "}/}/{gsub(/} /,"}\n");ORS="\n"}{print}' file.txt

gives output

qwe rty
uio {asd fgh jkl zxc}
vbn
mqw rty

Explanation: when encountering { I set output row separator (ORS), when encountering } I substitue } by } followed by newline in order to be compliant with desired output and set output row separator to newline. After doing all changes I print line to which trailing output row separator is added.

(tested in GNU Awk 5.1.0)

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

Solution in TXR Lisp:

(flow (get-string)
  (regsub #/{[^{]*}/ (op regsub "\n" " "))
  (regsub "} " "}\n")
  put-string)
1$ txr foldbrace.tl < data
qwe rty
uio {asd fgh jkl zxc}
vbn
mqw rty

We read the file as a string. Then we filter all brace enclosed substrings through a function which substitutes spaces for newlines. Then we filter the string to convert spaces after braces into newlines. Then put the string.

op denotes a partial application notation for writing functions, so (op regsub "\n" " ") means, roughly speaking, (lambda (arg) (regsub "\n" " " arg)). When the second argument of regsub is a function rather than a string item, it filters the matching text through that function and uses the result.

The flow macro puts an implicit op into all its arguments that are function calls, which is why op isn't used in (regsub #/{[^{]+}/ ...).

Kaz
  • 55,781
  • 9
  • 100
  • 149