[Solved] Script to split a file into two


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Script to split a file into two
# 1  
Old 08-02-2011
[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
# 2  
Old 08-02-2011
Code:
awk ' /^$/ { t++; next }
      !t   { print > "file1" }
      t    { print > "file2" }
' INPUTFILE

This User Gave Thanks to yazu For This Post:
# 3  
Old 08-03-2011
Hi Yazu,

yes ,its working fine
Many thanksSmilie

---------- 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

Code:
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

Last edited by radoulov; 08-03-2011 at 07:50 AM.. Reason: Code tags.
# 4  
Old 08-03-2011
The format to send mails is a bit tricky. The general invocation syntax is:

Code:
<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:

Code:
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
This User Gave Thanks to bakunin For This Post:
# 5  
Old 08-03-2011
MySQL

Hi Bakunin,

Its working fine,

Thanks a lotttttSmilie
# 6  
Old 08-04-2011
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 Smilie

Many thanks in advance..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a file into two different file using shell script

could you please provide solution to split file and load into different files in shell script. cat test.txt "01"|""emp_name"":ram|""emp_sal"":600000|""emp_value"":""CREDITCARD""|""1410"":] "02"|""emp_name"":syam|""emp_sal"":500000|""emp_value"":""voucher""|""1610"":]|""1510"":]... (7 Replies)
Discussion started by: shabeena
7 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Split one file into more than one file

Guys, I have an input file MGC001|108.28|-2.86489|100-120|MANGGAC MGC002|108.071|-2.69028|80-100|KELAPA KAMPIT MGC003|108.168|-2.97053|50-80|GANTUNG MGC007|108.192722222|-2.766138889|0-50|KELAPA KAMPIT MGC008|108.11075|-3.002666667|0-50|GANTUNG... (10 Replies)
Discussion started by: radius
10 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Perl Question - split function with csv file

Hi all, I have a csv file that appears as follows: ,2013/03/26,2012/12/26,4,1,"2017/09/26,5.75%","2017/09/26,1,2018/09/26,1,2019/09/26,1,2020/09/26,1,2021/09/26,1",,,2012/12/26,now when i use the split function like this: my @f = split/,/; the split function will split the data that is... (2 Replies)
Discussion started by: WongSifu
2 Replies

4. Shell Programming and Scripting

Split file based on file size in Korn script

I need to split a file if it is over 2GB in size (or any size), preferably split on the lines. I have figured out how to get the file size using awk, and I can split the file based on the number of lines (which I got with wc -l) but I can't figure out how to connect them together in the script. ... (6 Replies)
Discussion started by: ssemple2000
6 Replies

5. Shell Programming and Scripting

[Solved] Reading the last word in a file from a script

Hello everybody, My first time here and my english is not very good I hope you understand me. I'm trying to read a file that contains two zip archive names. Here my file content is: package1.zip package2.zip At the end of the line there is a \n character. I read this file from a... (2 Replies)
Discussion started by: Aurea
2 Replies

6. Shell Programming and Scripting

KSH script for split a txt file

I have a problem which I would like to solve by using UNIX power and inspired minds around world. Here is the problem I have a text file and it has data as follows 1X.....................1234567890123456789T1234598765XT1 (header) 1Z01............(sub HEADER) P100001............ Q1........... (4 Replies)
Discussion started by: ask.chowhan
4 Replies

7. Shell Programming and Scripting

[Solved] Split file using awk

hlow all, need your advice i have sample.txt 1252468812,yahoo,3.5 1252468812,hotmail,2.4 1252468819,yahoo,1.2 1252468812,msn,8.9 1252468923,gmail,12 1232468819,live,3.4 1252368929,yahoo,9.0 1252468929,msn,1.2now i want filtering with awk so output will like this 12524_log.txt... (2 Replies)
Discussion started by: zvtral
2 Replies

8. Shell Programming and Scripting

[Solved] perl and grep: get a script to look at more then one file

hi guys i have this very messy script, that looks in /var/log/messages.all for an error and reports if it finds the key works how can i get it to look at more then one file, i.e /var/log/message.all * so it looks in old logs as well thanks exit 0 if (isRenderNode(hostname)); my... (4 Replies)
Discussion started by: ab52
4 Replies

9. Shell Programming and Scripting

how to get split output of a file, using perl script

Hi, I have file: data.log.1 ### s1 main.build.3495 main.build.199 main.build.3408 ###s2 main.build.3495 main.build.3408 main.build.199 I want to read this file and store in two arrays in Perl. I have following command, which is working fine on command prompt. perl -n -e... (1 Reply)
Discussion started by: ashvini
1 Replies

10. Shell Programming and Scripting

Shell script to split XML file

Hi, I'm experiencing difficulty in loading an XML file to an Oracle destination table.I keep running into a memory problem due to the large size of the file. I want to split the XML file into several smaller files based on the keyword(s)/tags : '' and '' and would like to use a Unix shell... (2 Replies)
Discussion started by: bayflash27
2 Replies
Login or Register to Ask a Question