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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-01-2006, 12:20 PM
|
#1
|
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187
Rep:
|
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
|
|
|
|
03-01-2006, 02:02 PM
|
#2
|
|
Member
Registered: May 2003
Posts: 167
Rep:
|
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
|
|
|
|
03-01-2006, 10:37 PM
|
#3
|
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187
Original Poster
Rep:
|
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!
|
|
|
|
03-02-2006, 01:55 AM
|
#4
|
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
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
|
|
|
|
03-03-2006, 09:22 AM
|
#5
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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.
|
|
|
|
03-04-2006, 03:21 AM
|
#6
|
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
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! 
|
|
|
|
03-04-2006, 03:35 AM
|
#7
|
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
Absolutely, I cannot understand this,
could you please explain that?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:15 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|