Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

Split a text file in half (or any percentage) on Ubuntu Linux Split file teks di setengah (atau persentase) pada 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. Jika Anda memiliki berat file teks yang ingin Anda proses pemisahan di bagian kadang-kadang dapat membantu proses waktu, terutama jika kita akan mengimpor file ke spreadsheet. Atau Anda mungkin ingin hanya mengambil baris tertentu dari serangkaian file.

Enter split, wc, tail, cat, and grep. Masukkan split, wc, ekor, kucing, dan grep. (don't forget sed and awk). (jangan lupa sed dan awk). Linux contains a rich set of utilities for working with text files on the command line. Kaya linux berisi kumpulan utilitas untuk bekerja dengan file teks pada baris perintah. For our task today we will use split and wc. Untuk tugas kita hari ini kita akan menggunakan perpecahan dan wc.

First we take a look at our log file…. Pertama kita melihat file log kita ....

> 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. Kita melihat bahwa ukuran file 42MB. That's kinda big… but how many lines are we dealing with? Itu agak besar ... tapi berapa banyak baris yang kita hadapi? If we wanted to import this into Excel, we would need to keep it less than 65k lines. Jika kita ingin untuk diimpor ke Excel ini, kita harus tetap kurang dari 65k baris.

Let's check the amount of lines in the file using the wc utility, which stands for “word count”. Mari kita memeriksa jumlah baris dalam file dengan menggunakan utilitas wc, yang merupakan singkatan dari "word count".

> wc -l access.log > Wc-l access_log
146330 access.log 146.330 access_log

We're way over our limit. Kami jalan di atas batas kita. We'll need to split this into 3 segments. Kita akan perlu untuk membagi menjadi 3 segmen ini. We'll use the split utility to do this. Kita akan menggunakan utilitas split untuk melakukan hal ini.

> split -l 60000 access.log > Split-l 60.000 access_log
> ls -l > Ls-l

total 79124 total 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. Sekarang kami telah membagi file teks kita menjadi 3 file terpisah, masing-masing berisi kurang dari 60.000 baris, yang tampaknya seperti nomor yang baik untuk memilih. The last file contains the leftover amount. File terakhir berisi jumlah sisa. If you were going to cut this particular file in half, you'd have done this: Jika Anda akan memotong file khusus ini menjadi dua, Anda akan melakukan ini:

> split -l 73165 access.log > Split-l 73.165 access_log

And, that's all there is to it. Dan, itu semua ada untuk itu.

This article was originally written on 09/19/06 Tagged with: Artikel ini awalnya ditulis pada 09/19/06 Tagged with: Shell Scripts Shell Script , , Ubuntu Ubuntu

Daily Email Updates Harian Email Pembaharuan

You can get our how-to articles in your inbox each day for free. Anda bisa mendapatkan kita bagaimana-untuk artikel dalam kotak masuk Anda setiap hari secara gratis. Just enter your name and email below: Cukup masukkan nama dan email Anda di bawah ini:


Name: Nama:
Email: Email:

Comments (1) Komentar (1)

  1. Simon C. Ion Simon C. Ion

    If you have bc and sed installed, why not do this to calculate the halfway point of the file and perform the split? Jika Anda memiliki bc dan sed diinstal, mengapa tidak melakukan hal ini untuk menghitung titik setengah dari file dan melakukan 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 tampaknya default ke 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. Doa yang sed secara efektif bertindak sebagai panggilan ke lantai (3), melucuti menghilangkan angka setelah desimal, dan membuat versi saya membagi bahagia. I guess that the sed expression would need to be changed to 's/,.*//' for locales that use ',' as their “numbers after the decimal” indicator. Saya menduga bahwa ekspresi sed akan perlu diubah kepada 's /,.*//' untuk locales yang menggunakan', 'sebagai "angka setelah desimal" indikator.


Leave a Comment Tinggalkan Komentar




Leave your Meninggalkan friendly ramah comment here. komentar di sini.

If you have a computer help question, Jika Anda memiliki pertanyaan bantuan komputer, click here to leave it on the forums klik di sini untuk meninggalkannya di forum instead. selain.

Note: Your comment may not show up immediately on the site. Catatan: Komentar anda mungkin tidak muncul segera di situs.

Our Friends Our Friends
Getting Started Persiapan


About How-To Geek Tentang How-To Geek
What Is That Process? Apa Proses Itu?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
wmpnscfg.exe wmpnscfg.exe
rundll32.exe rundll32.exe
wfcrun32.exe wfcrun32.exe
Ipoint.exe Ipoint.exe
Itype.exe Itype.exe
Wfica32.exe Wfica32.exe
Mobsync.exe Mobsync.exe
conhost.exe conhost.exe
Dpupdchk.exe Dpupdchk.exe Adobe_Updater.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. Copyright © 2006-2009 HowToGeek.com. All Rights Reserved. All Rights Reserved.