search between keywords and make a single line

have a very big file where need to format it like below

example file:

abcd today
is
great
day;

search keyword 'abcd' and append to it all words till we reach ; to make it a single line.

output should look like.

abcd today is great day;

There are many occurrence of such paragraphs and text file also contain other lines which we do not want to touch.

Please do not try to oversimplify problems. State them as they stand. Also, provide a representative sample which shows all (or most of) the possible ways in which the input data might occur.

With lots of assumptions, try:

sed '/abcd/,/;/{
H
/;/{
      s/.*//
      x
      s/^\n//
      s/\n/ /g
      p
 }
d
}' file
1 Like
$
$
$ cat f14
abcd today
is
great
day;
abcd
another one
over
here;
some
text
here;
and some more...
$
$
$
$ perl -ne 'chomp;
            if (/abcd/)         {$x .= $_; $in = 1}
            elsif (/;/ and $in) {print "$x $_\n"; $in = 0; $x = ""}
            elsif ($in)         {$x .= " $_"}
            else                {print $_,"\n"}
           ' f14
abcd today is great day;
abcd another one over here;
some
text
here;
and some more...
$
$
$ perl -0ne 'while (/^(abcd.*?;|.*)$/msg) {$x=$1; $x =~ s/\n/ /g if $x =~ m/abcd/; print "$x\n"}' f14
abcd today is great day;
abcd another one over here;
some
text
here;
and some more...

$
$
$

tyler_durden

1 Like