I have a unix file like below
abcdge
efg
bh
ggh
bhj
mkl
I want to change the file and make it like:
abcdge
efg
bhggh
bhj
mkl
means: after each blank line, the next line should be append with previous line. How can i achieve it?
I have a unix file like below
abcdge
efg
bh
ggh
bhj
mkl
I want to change the file and make it like:
abcdge
efg
bhggh
bhj
mkl
means: after each blank line, the next line should be append with previous line. How can i achieve it?
$ sed -e '
$!N;$q;N
s/\n\n//
P;D
' file
Maintain 3 lines inn the pattern space and remove the consecutive newlines when seen.
This will work using any awk in any shell on every UNIX box:
$ awk '1;END{print"\n"}' RS= ORS= file
abcdge
efg
bhggh
bhj
mkl
1, mean?
{print $0} and you'll find it used in many, many awk scripts.
awk '1' RS= ORS= file; echo appears to work just as well
awk use.
If I understand you, maybe this is what you are looking for:
sed -z 's/\n\n//g' file
Output:
abcdge
efg
bhggh
bhj
mkl
sed read and process any text file as one "line". This may be sub-optimal (or fail?) if the file is huge. And the command cannot be used as a real-time filter for a "neverending" text stream.
<file awk '
{
if (n && $0!="")
printf "\n"
if ($0=="")
n=""
else
{n=1; p=1; printf "%s", $0;}
}
END {if (p) printf "\n"}
'
Each non-empty line is printed without its terminating newline character. Before this happens, if the current line is not empty and the previous line was not empty, a newline character gets printed; so in these circumstances the previous line becomes complete.
Finally, if any non-empty line was printed, one newline character terminates the last incomplete line and makes it complete.
n and p are markers indicating "non-empty last line" and "something was printed" respectively.
Consecutive empty lines are equivalent to one empty line. I don't know if this behavior is what you want.
The solution should work well as a filter (i.e. in a pipe). If you need it to modify the input file, check this: Save modifications in place with awk.
print "" instead of printf "\n" as the former just uses whatever value ORS has while the latter is hard-coding the value that ORS is assumed to have. And it saves you 3 chars :-).