1833053 Members
2287 Online
110049 Solutions
New Discussion

add new line script

 
SOLVED
Go to solution
george_57
Occasional Advisor

add new line script

Hi Experts,
Suppose if i have a file that contains 5 fields like :

350 34 1 2.1 3.1
350 35 1 2.2 2.3
350 36 1 2.3 1.3
400 37 1 2.4 5.3
400 38 1 2.5 2.2
401 39 1 2.6 3.5
401 40 1 2.7 5.1
401 41 1 2.8 2.2

And if i want to insert a blank line after every change in the first field for example in this case add a new line between 350 and 400 then 400 and 401 and so on for the whole file how do I do it. Either a one line awk or sed or a script ? Please help
none
5 REPLIES 5
Peter Kloetgen
Esteemed Contributor

Re: add new line script

Hi George,

you can manage this with a simple script:

for i in `cat input_file`
do
echo "$i \n"
done

---> echo " \n" will give an extra blank line after the original line....

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
george_57
Occasional Advisor

Re: add new line script

Hi Pete,
Thanks a lot for the response...but this didn't help...This gives a new line after every field. What i need is something like a blank line seperator after 3 records of 350, after 2 records of 400 and after 3 records of 401. i.e. After every change in the value of the first field. Please help...
none
john korterman
Honored Contributor
Solution

Re: add new line script

Hi,
you could try something like this:


#!/usr/bin/sh

PREVIOUS=""

cat $1 | while read a b c d e
do
if [ "$a" != "$PREVIOUS" ]
then
echo ""
fi
echo $a $b $c $d $e
PREVIOUS=$a
done



regards,

John K.
it would be nice if you always got a second chance
curt larson_1
Honored Contributor

Re: add new line script

give this a try
cat yourfile |
awk 'BEGIN { x = $1;}
{
y=$1;
if ( y == x ) print $0;
else {
print "\n$0";
x=y;
}
}
george_57
Occasional Advisor

Re: add new line script

Thanks a lot guys...it worked great....
none