Split a text file in half (or any percentage) on Ubuntu Linux भाजित छमाही में एक पाठ फ़ाइल (या कोई प्रतिशत Ubuntu Linux पर)
If you have an unwieldy text file that you are trying to process, splitting it in sections can sometimes help processing time, especially if we were going to import a file into a spreadsheet. Or you might want to just retrieve a particular set of lines from a file. यदि आप एक बोझल पाठ फ़ाइल है कि आप प्रक्रिया के लिए कोशिश कर रहे हैं, वर्गों में विभाजन कभी कभी प्रसंस्करण समय मदद कर सकता है, खासकर यदि हम एक स्प्रेडशीट में एक फ़ाइल आयात करने जा रहे थे या. तुम सिर्फ लाइनों के एक विशेष सेट से पुनः प्राप्त करना चाह सकते हैं एक फाइल.
Enter split, wc, tail, cat, and grep. विभाजन दर्ज करें, WC पूंछ, बिल्ली, और grep. (don't forget sed and awk). कर (और sed नहीं awk) भूल जाओ. Linux contains a rich set of utilities for working with text files on the command line. Linux कमांड लाइन पर पाठ फ़ाइलों के साथ काम करने के लिए उपयोगिताओं के एक अमीर सेट होता है. For our task today we will use split and wc. हमारे काम के लिए आज हम विभाजन और WC प्रयोग करेंगे.
First we take a look at our log file…. पहले हम हमारे लॉग फाइल को देखो ....
> ls -l रास>-L
-rw-r–r– 1 thegeek ggroup 42046520 2006-09-19 11:42 access.log -rw-r r-1 ggroup thegeek 42046520 2006-09-19 11:42 access.log
We see that the file size is 42MB. हम देखते हैं कि फ़ाइल आकार 42MB है. That's kinda big… but how many lines are we dealing with? वह थोड़े बड़े ... लेकिन लाइनों कितनी हम साथ काम कर रहे हैं? If we wanted to import this into Excel, we would need to keep it less than 65k lines. यदि हम एक्सेल में यह आयात करना चाहता था, हम उसे कम से कम 65k लाइनों रखने की आवश्यकता होगी.
Let's check the amount of lines in the file using the wc utility, which stands for “word count”. चलो फ़ाइल में लाइनों की मात्रा की जांच WC उपयोगिता है, जो 'के लिए शब्द गणना का उपयोग कर खड़ा है. "
> wc -l access.log > WC-L access.log
146330 access.log 146,330 access.log
We're way over our limit. हम अपनी सीमा के रास्ते पर हैं. We'll need to split this into 3 segments. हम से 3 क्षेत्रों में इस विभाजन की आवश्यकता होगी. We'll use the split utility to do this. हम अलग उपयोगिता का प्रयोग कर यह कर दूँगा.
> split -l 60000 access.log > विभाजन एल 60,000 access.log
> ls -l रास>-Ltotal 79124 कुल 79,124
-rw-rw-r– 1 thegeek ggroup 40465200 2006-09-19 12:00 access.log rw-rw-r-1 ggroup thegeek 40465200 2006-09-19 12:00 access.log
-rw-rw-r– 1 thegeek ggroup 16598163 2006-09-19 12:05 xaa rw-rw-r-1 ggroup thegeek 16598163 2006-09-19 12:05 xaa
-rw-rw-r– 1 thegeek ggroup 16596545 2006-09-19 12:05 xab rw-rw-r-1 ggroup thegeek 16596545 2006-09-19 12:05 xab
-rw-rw-r– 1 thegeek ggroup 7270492 2006-09-19 12:05 xac rw-rw-r-1 ggroup thegeek 7270492 2006-09-19 12:05 xac
We've now split our text files into 3 seperate files, each containing less than 60000 lines, which seemed like a good number to choose. अब हम अलग 3 फ़ाइलें, हमारे पाठ फ़ाइलों में विभाजित है कम से कम 60,000 लाइनों, जो कि एक अच्छी संख्या में चुनाव की तरह लग रहा था जिसमें प्रत्येक. The last file contains the leftover amount. पिछले फ़ाइल शेष राशि शामिल है. If you were going to cut this particular file in half, you'd have done this: यदि आप छमाही में इस विशिष्ट फ़ाइल काटा जा रहे थे, आप इस किया है चाहते हैं:
> split -l 73165 access.log > विभाजन एल 73,165 access.log
And, that's all there is to it. और, यह सब वहाँ यह है.

Daily Email Updates दैनिक ईमेल अपडेट
You can get our how-to articles in your inbox each day for free. आप अपने इनबॉक्स में हमारे कैसे लेख को मुफ्त के लिए प्रत्येक दिन प्राप्त कर सकते हैं. Just enter your name and email below: बस नीचे अपना नाम और ईमेल दर्ज करें:



If you have bc and sed installed, why not do this to calculate the halfway point of the file and perform the split? यदि आप और ई.पू. है sed स्थापित है, तो क्यों नहीं इस फाइल के आधे रास्ते बिंदु की गणना और विभाजन का पालन नहीं?
split -l $(echo $(cat tmp.txt | wc -l)/2 | bc -l | sed -e 's/\..*//') access.log विभाजन एल ($ $ (बिल्ली tmp.txt प्रतिध्वनि |-l) / 2 | ई.पू. एल WC sed |-ए 's / \ ..*//') access.log
NB: bc seems to default to a floating-point output. एनबी: ई.पू. एक चल बिन्दु उत्पादन के लिए डिफ़ॉल्ट लगता है. The sed invocation effectively act as a call to floor(3), stripping away the numbers after the decimal, and making my version of split happy. Sed मंगलाचरण प्रभावी ढंग से (3) मंजिल पर एक फोन के रूप में कार्य, दशमलव के बाद दूर अलग करना नंबर, और खुश विभाजन के अपने संस्करण बना. I guess that the sed expression would need to be changed to 's/,.*//' for locales that use ',' as their “numbers after the decimal” indicator. मुझे लगता है कि sed अभिव्यक्ति के लिए 'में परिवर्तित करने की आवश्यकता होगी स्थलों के लिए /,.*//' एस कि' उपयोग करते हैं, 'के रूप में उनके दशमलव सूचक "के बाद" संख्या.