-3

Need to join random number of lines untill close brac match and substitute with one space in unix file . There can be multiple occurence like this in file . One liner awk , sed , perl solution will be more helpful.

Eg :
....
abc_mod #(
.h_res(3),
.s_res(10)
)
u_abc_mod
(
....
....
....
def_mod #(
.lp(4),
.clk(9),
.add(5),
.d(8),
.por(1)
)
u_def_mod
(
....

Need to match pattern equivalent to #( till 1st close brace at starting of line ) followed by one extra new line (can include random number of lines ) and substitute with single space.

Desired Output :
....
abc_mod u_abc_mod
(
...
def_mod u_def_mod
(
...
...

Thanks

  • Using what tool? `sed`? `awk`? And remember that you can't match "till 1st close brace", because you have multiple parens inside there. You'll need to match until "line that starts with closed paren". – Tim Roberts May 16 '23 at 05:25
  • Thanks , Edited ! Any tool is fine untill the required output is available . – argha chakraborty May 16 '23 at 05:28
  • Can you please share using sed or awk or pcregrep/egrep , as it can be easily incorporated into csh script – argha chakraborty May 16 '23 at 06:19
  • Regarding "... incorporated into csh script" - see https://www.grymoire.com/unix/CshTop10.txt, http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/, etc. for why not to write scripts in csh. – Ed Morton May 17 '23 at 14:16

1 Answers1

1

Here's a Python solution:

import sys

skip = False
for line in sys.stdin:
    line = line.rstrip()
    if not skip:
        i = line.find('#(')
        if i >= 0:
            print(line[:i], end='')
            skip = True
        else:
            print(line)
    else:
        if line == ')':
            skip = False

Output:

$  python x.py < x.tzt
abc_mod u_abc_mod
(
....
....
....
)
def_mod u_def_mod
(
....
)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30