כיצד להשתמש בפקודות 'cat' ו-'tac' עם דוגמאות ב-Linux

המאמר הזה הוא חלק מסדרת השיטות והטיפות לLinux שלנו, במקרה זה, אנחנו נעיף על כמה שימושים ביסודיים בפקודת cat (הפקודה הכי נפוצה בLinux) ובפקודת tac (ההפך מפקודת cat – מקרין את הקבצים בסדר הפוך) בעזרת דוגמאות מעשיות.

קרא גם: 13 דוגמאות שימושיות של פקודת 'cat' בLinux

שימוש בפקודת Cat בLinux

פקודת Cat, האקרון שלה הוא קונקatenאציה, היא אחת הפקודות הכי נפוצות במערכות *nix. השימוש הכי בסיסי של הפקודה הוא לקריאת קבצים והצגתם ב stdout, כלומר להצגת התוכן של הקבצים על הטרמינל.

# cat file.txt
View Content of File in Linux

שימוש נוסף בפקודת cat הוא לקריאת או לצרף יחדיו מספר קבצים ולשלוח את התוצאה למסך כמו שמוצג בדוגמאות הבאות.

# cat file1.txt file2.txt file3.txt
View Content of Multiple Files

הפקודה יכולה גם להיות משמשת על מנת להדביק (לחבר) מספר קבצים לקבץ אחד בעזרת מסמל ההעברה הלינוקסי “>”.

# cat file1.txt file2.txt file3.txt > file-all.txt
Join Multiple Files in Linux

בעזרת מסמל ההוספה המתאים ניתן להוסיף את התוכן של קבץ חדש לראש התחתון של הקבץ file-all.txt בעזרת הסינTAX הבא.

# cat file4.txt >> file-all.txt
Append Content File to New File

הפקודה cat יכולה להיות משמשת להעתיק את התוכן של קבץ לקבץ חדש. הקבץ החדש יכול להיות משנה. לדוגמה, להעתיק את הקבץ מהמקום הנו

# cat file1.txt > /tmp/file1.txt 
Copy Content of File to New File

עוברים את הקובץ מהמיקום הנוכחי אל /tmp/ ומשנה את שמו.

# cat file1.txt > /tmp/newfile.cfg
Copy File to /tmp Location

A less usage of the cat command is to create a new file with the below syntax. When finished editing the file hit CTRL+D to save and exit the new file.

# cat > new_file.txt
Create New File using Cat Command

כדי למספר את כל השורות בקבץ, כולל השורות הריקים, משמשים את האפשרות -n.

# cat -n file-all.txt
Add Numbers to Lines in File

על מנת להראות רק את מספר כל שורה לא ריקה, משמשים את האפשרות -b.

# cat -b file-all.txt
Print Line Numbers in File

רוצה ללמוד עוד על הפקט הלינוקסי "cat"? אז קרא למאמר שלנו שעוסק ב 13 דוגמאות מועילות של הפקת 'cat' בלינוקס.

למדו איך להשתמש בפקט Tac בלינוקס

מצד שני, יש פקט פחות ידוע ופחות משומש במערכות *nix שנקרא tac. Tacהוא בעצם גירסה הפוכה של הפקת cat (גם היא מולטת לאחור) שמדפיסה כל שורה בקבץ מהשורה התחתונה ועד לשורה העליונה לפינת הקולט המקובל שלך.

# tac file-all.txt
Print Content File in Reverse Order

אחד האפשרויות החשובות ביותר של הפקט מיוצג באפשרות -s, שמפרידה את התוכן של הקבץ על פי סימן או מילה מסויימת בקבץ.

# tac file-all.txt --separator "two"
Remove Matching String in File

השימוש החשוב הבא בפקט tac הוא, שהוא יכול לסייע נהדר בעזרה לבדיקת קבצי לוג, על ידי הפיכת סדר הזמנים של תוכן הלוג בסדר הפוכה.

$ tac /var/log/auth.log

Or to display the last lines

$ tail /var/log/auth.log | tac
דוגמא יצוא
tecmint@tecmint ~ $ tac /var/log/auth.log
pr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root
....
tecmint@tecmint ~ $ tail /var/log/auth.log | tac
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 15:55:02 tecmint CRON[17194]: pam_unix(cron:session): session closed for user root
Apr  6 15:55:01 tecmint CRON[17195]: pam_unix(cron:session): session closed for user root
...

כמו פקודת cat, גם פקודת tac מבצעת עבודה מצוינת ב-עיבוד קבצי טקסט, אך יש להימנע ממנה בקבצים מסוגים אחרים, במיוחד קבצים בינאריים או בקבצים שבהם השורה הראשונה מציינת את התוכנית שתפעל אותם.

Source:
https://www.tecmint.com/learn-linux-cat-command-and-tac-command/