[Solved] Script to split a file into two

Hi i have a file like

a 12
b 13

c 14
d 15

I want to split it based on a blank line like in first file I should have
a 12
b 13
and in the second file I have
c 14
d 15
How can i do this? Any help will be greatly appreciated

awk ' /^$/ { t++; next }
      !t   { print > "file1" }
      t    { print > "file2" }
' INPUTFILE
1 Like

Hi Yazu,

yes ,its working fine
Many thanks:b:

---------- Post updated 08-03-11 at 04:48 AM ---------- Previous update was 08-02-11 at 06:02 AM ----------

Hi,
This is working fine but it might be possible that files are already splitted,in that case I have to send a mail to an address,I searched for that in this forum and found some information on that, but using all those also i cant implement the logic of sending the mail through script

this is what I have tried

a=2
b=` ls | grep 'Fin' | wc -l`
if [ $b = $a ]
then
mailx -s "Subject"  xx@yy.com
else
awk ' /^$/ { t++; next }
      !t   { print > "A.Txt" }
      t    { print > "B.TXt" }
' FinFile.txt
exit 1
fi

When i try to execute this its not doing anything ,when i tried by using sh -x ,
I can see its stopped at the mail line

need your help.

Many thanks in advance

The format to send mails is a bit tricky. The general invocation syntax is:

<process generating mail text> | mail -s "<Subject line>" <recipients address>">

So, for example, if you want to send a mail with the text "Hello World." and the subject "first attempt" to "person@server.org" you would write:

echo "Hello World." | mail -s "first attempt" person@server.org

Your problem was that there was no text body you tried to send.

I hope this helps.

------

PS: On a second thought: Do you have some sort of mail daemon installed and running on your system? You will have to install this to be able to send mails.

bakunin

1 Like

Hi Bakunin,

Its working fine,

Thanks a lottttt:b:

Hi all,

I have an another issue in the new files,I am struggling to remove carriage return at the end of file,because due to that i am getting a extra line at the end of file,I tried the below
commands
cat filename | sed 's/.$//' > file1

and

sed 's!\\r\\r\\n!\\r\\n!g' temp5.txt > temp7.txt

But i am not able to remove it,not sure where i am doing wrong?

Please help :wall:

Many thanks in advance..