Split a text file in half (or any percentage) on Ubuntu Linux Split tekstinį failą per pusę (ar bet koks procentas) 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. Jei turite painios tekstinis failas, kurį bandote procesą, išskaidyti jį į dalis, kartais gali padėti apdorojimo laikas, ypač jeigu mes ketiname importuoti failą į skaičiuoklę. Arba galite tiesiog paimti tikri eilutės iš failas.
Enter split, wc, tail, cat, and grep. Įveskite Split, WC, uodega, katinas, ir grep. (don't forget sed and awk). (nepamirškite Sed ir awk). Linux contains a rich set of utilities for working with text files on the command line. Linux yra turtingas rinkinys priemonių, skirtų dirbti su teksto failus naudodami komandų eilutę. For our task today we will use split and wc. Mūsų uždavinys šiandien mes naudosime Split ir WC.
First we take a look at our log file…. First We Take mūsų žurnalo failą atrodo ....
> ls -l > Ls-l
-rw-r–r– 1 thegeek ggroup 42046520 2006-09-19 11:42 access.log -rw-r-r-1 thegeek ggroup 42046520 2006-09-19 11:42 access.log
We see that the file size is 42MB. Mes matome, kad failo dydis 42Mb. That's kinda big… but how many lines are we dealing with? Tai kažkas didelis ... bet kiek eilučių yra mums spręsti? If we wanted to import this into Excel, we would need to keep it less than 65k lines. Jei norime importuoti į tai Excel ", mums reikia jį laikyti mažiau negu 65k linijos.
Let's check the amount of lines in the file using the wc utility, which stands for “word count”. Let's patikrinti eilučių kiekis failą naudojant WC naudingumą, kuris reiškia "Word count".
> wc -l access.log > Wc-l access.log
146330 access.log 146.330 access.log
We're way over our limit. Mes taip per mūsų ribos. We'll need to split this into 3 segments. Mes reikia padalinti į tai 3 segmentus. We'll use the split utility to do this. Mes naudosime Split naudingumas tai padaryti.
> split -l 60000 access.log > Split-l 60.000 access.log
> ls -l > Ls-ltotal 79124 viso 79.124
-rw-rw-r– 1 thegeek ggroup 40465200 2006-09-19 12:00 access.log -rw-rw-r-1 thegeek ggroup 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 thegeek ggroup 16598163 2006-09-19 12:05 xaa
-rw-rw-r– 1 thegeek ggroup 16596545 2006-09-19 12:05 xab -rw-rw-r-1 thegeek ggroup 16596545 2006-09-19 12:05 xab
-rw-rw-r– 1 thegeek ggroup 7270492 2006-09-19 12:05 xac -rw-rw-r-1 thegeek ggroup 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. Dabar mes padalinti savo tekstinius failus į 3 atskirus failus, kurių kiekviename yra mažiau kaip 60000 eilučių, kuris atrodė nemažai rinktis. The last file contains the leftover amount. Paskutinis failas yra likusių suma. If you were going to cut this particular file in half, you'd have done this: Jei buvo ketinama sumažinti šį failą per pusę, jūs turite padaryti tai:
> split -l 73165 access.log > Split-l 73.165 access.log
And, that's all there is to it. Ir, kad viskas yra prie jo.

Daily Email Updates Dienos paštas Atnaujinimai
You can get our how-to articles in your inbox each day for free. Galite gauti mūsų kaip prie straipsnių į Jūsų pašto dėžutę kasdien nemokamai. Just enter your name and email below: Tiesiog įveskite vardą ir elektroninio pašto adresą žemiau:



If you have bc and sed installed, why not do this to calculate the halfway point of the file and perform the split? Jei turite BC ir sed įrengtas, kodėl gi ne tai apskaičiuoti pusiaukelėje į failą ir vykdyti Split?
split -l $(echo $(cat tmp.txt | wc -l)/2 | bc -l | sed -e 's/\..*//') access.log Split-l $ (echo $ (cat tmp.txt | wc-l) / 2 | BC-l | sed-e 's / \ ..*//') access.log
NB: bc seems to default to a floating-point output. NB: BC atrodo nutylėjimą slankiojo kablelio rezultatas. 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 pritaikymo veiksmingai veikti kaip raginimą aukšte (3), valymo toli skaičių po kablelio, ir todėl mano versija Split laimingas. I guess that the sed expression would need to be changed to 's/,.*//' for locales that use ',' as their “numbers after the decimal” indicator. Manau, kad sed išraiška turėtų būti pakeista į "s /,.*//' už locales, kad naudoti", "kaip savo" skaičių po dešimtainio "indikatorius.