Operating System - HP-UX
1753839 Members
8602 Online
108806 Solutions
New Discussion юеВ

Help to split a file into multiple files

 
SOLVED
Go to solution
Praveen Hari
Advisor

Help to split a file into multiple files

Hi,

I have a Large Newsgroup file in rnews format.
I need to split messages in this file into sub files so that each message in a seperate file.
Please see below for file format. Each message starts with a line "#! rnews 0". I want to read from that line to next "#! rnews 0" not including the "#! rnews 0". The message number is displayed at the end of line starting "xref". I want to name the file as this number. Any help from AWK/SED Guru's are appreciated.

#! rnews 0
Newsgroups: xxx
From: xxx
Subject: xxx
Date: xxx
Xref: linux-nntp sybase.test:6

zzzzzzzzzzzzzzzzzzzzz


#! rnews 0
Newsgroups: xxx
From: xxx
Subject: xxx
Date: xxx
Xref: linux-nntp sybase.test:7
zzzzzzzzzzzzzzzzzzzzz

7 REPLIES 7
Michael Tully
Honored Contributor

Re: Help to split a file into multiple files

Have you looked at the split/csplit commands ?
Anyone for a Mutiny ?
Praveen Hari
Advisor

Re: Help to split a file into multiple files

I have never used csplit. Cany you help me with some code snippets.
I am not expert in Shell scripts
Thanks
H.Merijn Brand (procura
Honored Contributor

Re: Help to split a file into multiple files

# perl -ne'BEGIN{$/="#! rnews 0\n"}chomp;$_||next;open STDOUT,">news.".++$i' file

untested

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James A. Donovan
Honored Contributor
Solution

Re: Help to split a file into multiple files

Try:

csplit -k myfile '/#! rnews 0/' '{*}'
rm xx00

for file in $(ls xx??);do
newname=$(grep ^Xref ${file}|awk -F":" '{print $3}')
mv ${file} ${newname}
done
Remember, wherever you go, there you are...
Michael Schulte zur Sur
Honored Contributor

Re: Help to split a file into multiple files

Hi,

try this also.

greetings,

Michael
Michael Schulte zur Sur
Honored Contributor

Re: Help to split a file into multiple files

Hi,

my last solution had a little flaw.
It forgot the text of the news. :-(
try this one.

have fun,

Michael
Praveen Hari
Advisor

Re: Help to split a file into multiple files

csPlit works fine.
I need help to do following in each file:
Each file has format:

#! rnews 0
Newsgroups: xxx
From: xxx
Subject: xxx
Date: xxx
Path: linux-nntp!forums-master!forums-1-dub!forums-1-dub!forums-master.sybase.com!forums-1-dub.sybase.com
Xref: linux-nntp sybase.test.xml:99
zzzzzzzzzzzzzzzzzzzzz

1)I need to replace "linux-nntp" in the line starting XRef: to "test".
2)I need to extract the number after sybase.text.xml from the line starting XRef: to a variable
3)Also I need to delete the text "linux-nntp!forums-master!" from the line starting "Path:"

Can soem one provide scripts for doing these operations.
Thanks