[SOLVED] split files by specifying a string (bash shell)
ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a file of around 300 lines in which string "SERVER" occurs around 32 times.
for eg.
Quote:
SERVER
.....
.....
....
SERVER
.....
.....
....
SERVER.....
.....
....
I need to split files like, for eg
Quote:
file1
SERVER
....
....
....
file2
SERVER
.....
...
....
file3
SERVER
.....
....
....
I am using this code
awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final
But the problem is that it makes maximum of 10 files, but I need more than 30.
I have tried using nawk, but didnt worked.
I am using bash scripting on Sun OS.
Yeah, there is a limit in solaris awk for the number of simultaneously opened files, but using nawk you can close the file terminating the streaming output. In this way you can write an unlimited number of files. I am thinking about something like this:
ghostdog74, maybe I am missing something, but what is the improvement of your replay in respect of mine? "Use indentation"?! "Don't use external variables"?! ...
ghostdog74, maybe I am missing something, but what is the improvement of your replay in respect of mine? "Use indentation"?! "Don't use external variables"?! ...
hmm..let's see, one extra call to sprintf() ? may not be significant in this case. just that i like to keep it simple, that's all. And yes, since you mentioned indentation, it also play a part, for readability.
Last edited by ghostdog74; 10-30-2007 at 09:00 AM.
Hmmm... it looks like you've looked for details after my last post and not really cared about the previous solution. The same for editing your code, by removing an unuseful part. The call to sprintf in my contribution just makes the filenames of the same length. Anyway, I am not questioning about the code, the solution, nor about your ability in awk programming. I saw other posts from you and you're really good. No kiddin'. What makes me angry sometimes, is the fact that a lot of people reply to the OP without caring about replies from other members. This is a little unfair since every little contribution is worth to be read before evaluating that something more can be told: comments, add-ons, notes, disapprovals... whatever! Maybe, just my personal opinion.
wow you are so sensitive and so serious...relax....i sometimes feel the same way, but so what?! life goes on. an example, sometimes, you know someone posted a homework/assignment, and you refrain from answering, but so what ??, someone will just come in and provide an answer...should i get angry every time such things happen? i just move on.. there are more important things to do in life than be concerned about minor things such as this.
... What makes me angry sometimes, is the fact that a lot of people reply to the OP without caring about replies from other members. This is a little unfair since every little contribution is worth to be read before evaluating that something more can be told: comments, add-ons, notes, disapprovals...
I agree with the first statement insofar that it can be frustrating, but it hasn't made me angry -- I see it as a possible form of rudeness -- possible because we don't know if it's intentional or not.
I don't agree that every reply is a worthwhile contribution, because they can be inane, erroneous, malicious, off-topic, etc.
As ghostdog74 says -- just move on, or do some yoga, get a drink of water, whatever helps you relax ... cheers, makyo
Command csplit was invented to solve these kinds of problems generally:
Code:
#!/usr/bin/env sh
# @(#) s2 Demonstrate csplit, context split.
set -o nounset
echo
debug=":"
debug="echo"
## Use local command version for the commands in this demonstration.
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash csplit edges my-nl
echo
# Remove previous debris.
rm -f xx*
echo " Current files, sample of final:"
ls -og final xx*
echo
edges -l 2 final
# Replace "*" with "99" for older versions of csplit.
csplit -k -q final /SERVER/ '{*}'
echo
echo " Result files and sample:"
ls xx*
my-nl xx11
exit 0
Producing:
Code:
% ./s2
(Versions displayed with local utility "version")
GNU bash 2.05b.0
csplit (coreutils) 5.2.1
edges (local) 287
my-nl (local) 294
Current files, sample of final:
ls: xx*: No such file or directory
-rw-r--r-- 1 866 Oct 29 05:38 final
1 SERVER
2 End of section 1
...
69 SERVER
70 End of section 35
Result files and sample:
xx00 xx03 xx06 xx09 xx12 xx15 xx18 xx21 xx24 xx27 xx30 xx33
xx01 xx04 xx07 xx10 xx13 xx16 xx19 xx22 xx25 xx28 xx31 xx34
xx02 xx05 xx08 xx11 xx14 xx17 xx20 xx23 xx26 xx29 xx32 xx35
==> xx11 <==
1 SERVER
2 End of section 11
I tried lots of commands including the above ones. They all run fine on Linux machines
BUT not on the solaris machines, dont know the reason behind it.
Anyways, MANY MANY THANKS to all for your time and help. I found this command to work perfectly.
Code:
/usr/xpg4/bin/awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.