LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-01-2006, 12:20 PM   #1
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Rep: Reputation: 30
Script Question: Spliting A File


Hello,

Here's a brain teaser for everyone. I have a file that has several sections in it that I need to split out into smaller files. Here is an example of such a file:

Code:
BLAH BLAH BLAH
BLAH

MORE BLAH MORE BLAH MORE BLAH
MORE BLAH MORE BLAH MORE BLAH

UPDATED BLAH

BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH

$$

MORE BLAH MORE BLAH MORE BLAH
MORE BLAH MORE BLAH MORE BLAH

UPDATED BLAH

BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH

$$

MORE BLAH MORE BLAH MORE BLAH
MORE BLAH MORE BLAH MORE BLAH

UPDATED BLAH

BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH

$$

MORE BLAH MORE BLAH MORE BLAH
MORE BLAH MORE BLAH MORE BLAH

UPDATED BLAH

BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH
BLAH BLAH BLAH BLAH BLAH BLAH BLAH BLAH

$$

BLAH
I need the stuff between the $$ as seperate files.

I'm going to tackle this using PHP but I was wondering if someone could provide a bash,gawk or perl way.

-- Tony


-- Tony
 
Old 03-01-2006, 02:02 PM   #2
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
There is probably an easier way to do this, but here is a bash script I wrote that seems to accomplish the task. The script takes the name of the file you want to separate as an argument.

Code:
#!/bin/bash
# Separate single large file into multiple smaller files
# using $$ as the delimiter

INPUT=$1

if [ -z $INPUT ]; then
echo no argument was passed to the script
echo exiting...
exit 1
fi

if [ ! -e $INPUT ]; then
echo the filename passed does not exist
echo exiting...
exit 1
fi

FILES=`sed -n '/\$\$/ p' input | wc -l`

cp ${INPUT} ${INPUT}.orig
cp ${INPUT} ${INPUT}.cp

for i in `seq 0 ${FILES}`
do
sed -n '1,/\$\$/ p' $INPUT | sed -e '$ d' > ${INPUT}${i}
sed -e '1,/\$\$/ d' ${INPUT}.cp > $INPUT
cp $INPUT ${INPUT}.cp
done

cp ${INPUT}.orig ${INPUT}
rm -f ${INPUT}.cp ${INPUT}.orig

exit 0
 
Old 03-01-2006, 10:37 PM   #3
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Original Poster
Rep: Reputation: 30
Does exactly what I wanted!

Thanks a lot! I spent a huge portion of the day doing this in PHP ... your script does it much nicer and cleaner. Thanks!
 
Old 03-02-2006, 01:55 AM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
here's a much more simpler way,

Code:
# !/usr/bin/ksh

cnt=1

while read line
do
if [ "$line" = "\$\$" ]
then
echo "equal"
cnt=$(($cnt + 1))
else
echo $line >> file$cnt
fi
done < inputfile

exit 0
 
Old 03-03-2006, 09:22 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
my go
Code:
#!/usr/bin/perl -w
$/ = '$$';
@F = <>;

foreach $n (0..$#F) {
        print "writing: $n\n";
	open IN, ">$n" or die "cannee do $n:$!";
        print IN $F[$n];
}
file on command line or pipe

Last edited by bigearsbilly; 03-03-2006 at 09:24 AM.
 
Old 03-04-2006, 03:21 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
Code:
#!/bin/bash
while [ "$(grep '\$\$' file)" ]
  do
  ((COUNT++))
  sed -n '1,/\$\$/p' file >file$COUNT
  sed -i '1,/\$\$/d' file
done
BTW kshkid ksh is EVIL!
 
Old 03-04-2006, 03:35 AM   #7
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
BTW kshkid ksh is EVIL!
Absolutely, I cannot understand this,
could you please explain that?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem spliting file to fit into a dvd Paxmaster Linux - Software 5 09-18-2005 06:03 PM
Script File Question terdbird Slackware - Installation 11 04-27-2005 12:45 PM
saving log files / spliting ethernet without a router w/ linux aarond Linux - Security 2 07-31-2004 02:34 PM
spliting a simple string kubicon Linux - General 2 02-27-2004 04:11 AM
script question? read from a file myketech Linux - General 5 03-06-2003 07:58 PM


All times are GMT -5. The time now is 01:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration