That's the job for sed
:
sed -e 'G;G;G;G;G' file
With awk
:
nawk -vORS='\n\n\n\n\n\n' 1 file
Or shorter version:
awk 'ORS="\n\n\n\n\n\n"' file
or avoid setting ORS
for each input line:
awk 'BEGIN{ORS="\n\n\n\n\n\n"};1' file
Here you go with awk
and processing the file only once.
awk -F'|' 'NR==1{print;next} m && NF{print m}
NF{l="\n"$0; $5=$4; m="\n"$0; c=0}; !NF{c++}
END{ print l; for (; i++<c;)print }' OFS='|' infile
Explanation:
Here we are skyping first line to being replace 5th field's value with 4th field's value, and just print it and do next
.
... if it (current next line) was not empty line (at least contains one field NF
), then take a backup of whole line with a \n
ewline added l="\n"$0
first next set 5th field's value with 4th field's value $5=$4
and last set it to a variable m
with a \n
ewline added m="\n"$0;
; There is a c
variable as a counter flag and is used to determine the number of empty lines !NF{c++}
if no line with at least one field seen; Otherwise c=0
will reset this counter.
Now we have modified line in m
variable and m && NF{print m}
will print it where in the next step awk
runs and m
has set and it's not on empty lines & NF
(this is used to prevent duplication on printing when empty line).
At the end we are printing the untouched last line which we take backup every time before performing replacement END{ print l; ...
and then number of empty lines which never seen a line with a field with looping for (; i++<c;)print }'
.
That's much shorter if you don't need redundant empty lines.
awk -F'|' 'NR==1{print;next} m && NF{print m}
NF{l=$0; $5=$4; m=$0} END{ print l}' OFS='|' infile
Best Answer
One way:
From man page of cat :
Once
cat
has squeezed the blank lines,sed
replaces the blank with with a---