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 פיצול קובץ טקסט במחצית (או כל אחוז) על אובונטו לינוקס

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. לינוקס מכילה מערך עשיר של כלי עזר לעבודה עם קבצי טקסט על שורת הפקודה. For our task today we will use split and wc. עבור הפעילות היום נשתמש מפוצלים WC שלנו.

First we take a look at our log file…. ראשית אנחנו נסתכל קובץ היומן שלנו ....

> 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. אנו רואים כי גודל הקובץ הוא 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. אם אנחנו רוצים לייבא את זה לתוך Excel, היינו צריכים לשמור את זה פחות מ 65 אלף קווים.

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 > לפצל-L 60000 access.log
> ls -l > Ls-l

total 79124 סה"כ 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. יש לנו עכשיו לפצל קבצי טקסט שלנו לתוך 3 קבצים נפרדים, כל אחד המכיל פחות מ 60000 שורות, שנראה כמו מספר טוב לבחור. 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 > לפצל-L 73165 access.log

And, that's all there is to it. וכן, זה כל מה שיש בו.

This article was originally written on 09/19/06 Tagged with: מאמר זה נכתב במקור ב 09/19/06 עם שתייגת: Shell Scripts סקריפטים Shell , , Ubuntu Ubuntu

Daily Email Updates שערי עדכונים בדוא"ל

You can get our how-to articles in your inbox each day for free. אתה יכול לקבל כמה שלנו למאמרים לתיבת הדואר שלך בכל יום בחינם. Just enter your name and email below: פשוט להזין את שם ואת הדוא"ל שלך להלן:


Name: שם:
Email: דואר אלקטרוני:

Comments (1) תגובות (1)

  1. 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? אם יש לך לפנה"ס sed מותקן, למה לא לעשות את זה כדי לחשב את נקודת אמצע הדרך של הקובץ ולבצע את הפיצול?

    split -l $(echo $(cat tmp.txt | wc -l)/2 | bc -l | sed -e 's/\..*//') access.log הפיצול-L $ (echo $ (tmp.txt חתול | WC-L) / 2 | bc-l | sed-e 's / \ access.log ..*//')

    NB: bc seems to default to a floating-point output. נ.ב.: BC נראה ברירת מחדל פלט נקודה צפה. 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 היה צריך להיות שונה ל 's /,.*//' עבור locales שהשימוש', 'כמו "המספרים שלהם אחרי מחוון העשרונית".


Leave a Comment השאירו תגובה




Leave your השאירו שלך friendly ידידותי comment here. תגובה כאן.

If you have a computer help question, אם יש לך מחשב לעזור השאלה, click here to leave it on the forums לחץ כאן כדי להשאיר אותו על פורומים instead. במקום.

Note: Your comment may not show up immediately on the site. הערה: ההערה שלך לא תופיע מיד באתר.

Our Friends חברים שלנו
Getting Started התחלת העבודה


About How-To Geek אודות How-To Geek
What Is That Process? מה זה תהליך?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe Ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
mDNSResponder.exe mDNSResponder.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. זכויות יוצרים © 2006-2009 HowToGeek.com. All Rights Reserved. כל הזכויות שמורות.