Shell Tip: cut

הפקודה משמשת לחיתוך שדות פלט עפ"י תוחמים (בעזרתם קובעים באיזה שדות/עמודות יתבצע החיתוך). כאמור ההסברים לקוחים מהפאלם האישי ולכן כתובים באנגלית

cut
The cut command has the ability to cut out characters or fields.
cut use delimiters to determine where to split the  fields.

By default, cut's delimiters are stored in the shell variable IFS (Input Field Separators).
Typing:

set | grep IFS

will show what the separator characters currently are; at present, IFS is either:
a. tab
b. new line
c. space.

Usage:
cut [-d char] [-c|-f] list [files...]
-d: define field delimiter (tab by default)
-c list:  cut by column position
-f list: cut by field number

Examples:

cat /etc/passwd | cut -d:  -f1,7
or:
cut -d:  -f1,7 /etc/passwd

will display:
field 1 (username)
field 7 (default shell)
from /etc/passwd.

cut -d:  -f1,6-7 /etc/passwd
will display:
field 1 (username)
field 6 (home directory)
field 7 (default shell)ut

who | cut -d' ' -f1
will display the username that
currently login to the system

cut -c1-7 /etc/tstab
will display the first 7 characters
in /etc/fstab (for each line)

My Signature
הפוסט הזה פורסם בקטגוריה טיפים לשורת הפקודה, לינוקס ותוכנה חופשית.‏ קישור ישיר לפוסט.

תגובה אחת על Shell Tip: cut

  1. מאת תייש‏:

    בהחלט אחת הפעולות שאני משתמש בהם רבות
    כמובן לזה מצטרף פעמים רבות גם:
    awk {'print $3'}
    a (האות כדי לישר את הטקסט)אני משתמש בשניהם בצורה רבה
    סיפרו לי כי ניתן לשלב בין השניים ואז לקצר את הכתיבה ולהוציא קודים יותר אפקטיבים.

כתיבת תגובה

האימייל שלך לא יוצג באתר. (*) שדות חובה מסומנים

*

תגי HTML מותרים: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>