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 am trying to split a large file into fragments. Within the text file there is a string recurring thousands of times and I wish to split it every 300 appearances. I have used csplit before, but I don't know (if I can) how to tell csplit to skip certain appearances of the string.
Alternatively, I thought of reading the file line by line, echo each line into a new file, and count each appearance of the string. When the count is 300, echo the lines into a new file (and restart the count). My problem is that I only know to count the total appearances of the string in the file using grep -c.
Can I count the appearances of the string "line by line"? (with awk maybe?) Alternatively, can I count the string using grep but only with with the first x lines of the file?
I've been using csh for this script.
This seems as a very inefficient method so more elegant ways are welcome. Thanks!
Thanks for the reply and the code. I am not familiar with bash scripts but I kind of get it. I replaced in.file with my input file, the string with my string (and added another " after the string for the script to run). However, something goes wrong with COUNT becasue it stays 0. The script creates only one file (1.file) which has a much higher than 300 apperances of the string. Though I understand what you wrote I can't find why wouldn't COUNT increase by 1.
Any ideas?
Quote:
Originally Posted by gnashley
Bash or (maybe) sh:
Code:
COUNT=0
OUT=1
while read LINE ; do
case $LINE in
*"string*) echo $LINE >> out.file ; ((COUNT++)) ;;
*) [ $COUNT -lt 300 ] && echo $LINE >> $OUT.file
esac
if [ $COUNT -eq 300 ] ; then
COUNT=0
((OUT++))
fi
done< in.file
unless you only have the shell to work with, otherwise, use awk (or other languages) good at parsing big files
Code:
# assuming pattern searched at every line, regardless of how many times it appears on the line
awk '/pattern/{++c}c==300{p++;c=0}{print $0 > "file_"p".txt" }' file
Last edited by ghostdog74; 12-22-2009 at 06:34 PM.
Thanks for the reply. I should be able to put this line in my csh shell, right?
I tried to do that, but I am getting an error saying:
Missing }.
Missing }.
awk: file
awk: ^ syntax error
where file is my input file (the last word in your code). Any ideas where the error is?
Thanks for the help.
Quote:
Originally Posted by ghostdog74
unless you only have the shell to work with, otherwise, use awk (or other languages) good at parsing big files
Code:
# assuming pattern searched at every line, regardless of how many times it appears on the line
awk '/pattern/{++c}c==300{p++;c=0}{print $0 > "file_"p".txt" }' file
Sorry for the previous. it is working perfectly! (I had an error with my ` ' ").
Thanks a lot!
Quote:
Originally Posted by ghostdog74
unless you only have the shell to work with, otherwise, use awk (or other languages) good at parsing big files
Code:
# assuming pattern searched at every line, regardless of how many times it appears on the line
awk '/pattern/{++c}c==300{p++;c=0}{print $0 > "file_"p".txt" }' file
It works perfect, but the first file that is generated by the script is called file_.txt (without a number). All the following files are numbered from 1 (file_1.txt) and up. I tried to enter p=1 in a few places (so the counting will start p=1 and not from an empty p), but couldn't make it to work. Any advice?
Quote:
Originally Posted by ghostdog74
unless you only have the shell to work with, otherwise, use awk (or other languages) good at parsing big files
Code:
# assuming pattern searched at every line, regardless of how many times it appears on the line
awk '/pattern/{++c}c==300{p++;c=0}{print $0 > "file_"p".txt" }' file
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.