متقدم
البحث فيالملخصات
طريقة البحث
  • الكلمات تجد عناوين الدروس والنص والكود. صفِّ حسب النوع (أدلة، دورات، برامج، كتب) أو ابقَ في هذا الكتاب.
  • book:rhcsa و ch:8 يضيّقان البحث؛ ‎#code يجد الكلمة داخل أوامر الدروس فقط.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الفصل 20الاسم في الكتاب: Text Processing

Process text

On Unix almost everything is text: settings, logs, web pages, emails, source code, CSV data. A set of small tools reads, sorts, cuts, joins, compares and changes text. This chapter covers cat, sort, uniq, cut, paste, join, comm, diff, patch, tr, sed and aspell.

  • 13 الدروس
  • 12 الأوامر
  • 1 المعامل

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

المهارات في هذا الفصل

  • فرز النصوص وقصّها ومقارنتها وتعديلها بالأدوات الكلاسيكية

20.1

Know where text is used

Text is the format of documents, web pages (HTML), email, printer output and program source code. That is why the text tools are so important.

20.2

Look closer with cat

cat has options that show hidden characters: tabs, line ends and repeated empty lines.

cat > foo.txt
	The quick brown fox jumped over the lazy dog.
cat -A foo.txt
^IThe quick brown fox jumped over the lazy dog.   $
cat -ns foo.txt
OptionWhat it does
-AShow all: tabs as ^I, line ends as $
-nNumber the lines
-sSqueeze many empty lines into one

ملاحظة

Windows text files end lines with CR+LF (^M$ in cat -A), Unix files only with LF. dos2unix and unix2dos convert them.

الأوامر في هذا الدرس

20.3

Sort lines with sort

sort sorts lines. It can sort by number, by a field (column), and merge sorted files.

OptionWhat it does
-bIgnore spaces at the start
-fIgnore case
-nSort by number
-hSort human sizes (2K, 1G)
-rReverse
-k fieldSort by a field (-k 3, -k 3.7 = from character 7 of field 3)
-t charThe field separator (default: spaces/tabs)
-mMerge sorted files
-o fileWrite to a file (can be the input file)
-uRemove duplicate lines
du -s /usr/share/* | sort -nr | head
ls -l /usr/bin | sort -nrk 5 | head
sort -t ':' -k 7 /etc/passwd | head
sort -k 1,1 -k 2n distros.txt
sort -k 3.7nbr -k 3.1nbr -k 3.4nbr distros.txt
  • -k 1,1 sorts only by field 1. -k 1 would use field 1 to the end of the line.

20.4

Report repeated lines with uniq

uniq removes or reports repeated lines. The input must be sorted.

OptionWhat it does
-cCount each line
-dOnly the repeated lines
-uOnly the lines that appear once
-iIgnore case
-f nSkip the first n fields
sort foo.txt | uniq
sort foo.txt | uniq -c | sort -rn
cut -d: -f7 /etc/passwd | sort | uniq -c

20.5

Cut columns with cut

cut takes parts of each line: characters (-c) or fields (-f) separated by a character (-d, default tab).

cut -f 3 distros.txt
cut -f 3 distros.txt | cut -c 7-10
cut -d ':' -f 1 /etc/passwd | head -3
root
bin
daemon
expand distros.txt | cut -c 23-
  • expand changes tabs into spaces; unexpand does the opposite.

الأوامر في هذا الدرس

20.6

Put lines together with paste and join

paste puts files side by side (as columns). join joins lines of two files that have the same key field, like a database.

cut -f 1,2 distros.txt > distros-names.txt
cut -f 3 distros.txt > distros-dates.txt
paste distros-dates.txt distros-names.txt
join distros-key-names.txt distros-key-vernums.txt | head
  • Both files must be sorted on the key field for join.

الأوامر في هذا الدرس

20.7

Reverse lines and characters with tac and rev

tac prints the lines of a file in reverse order (last line first). rev reverses the characters of each line.

tac /var/log/dnf.log | head
echo "hello" | rev
olleh

20.8

Compare files with comm and diff

comm compares two sorted files line by line. diff finds all differences and is used to make patches.

comm file1.txt file2.txt
comm -12 file1.txt file2.txt
diff file1.txt file2.txt
1d0
< a
4a4
> e
diff -u file1.txt file2.txt
  • comm prints 3 columns: only in file 1, only in file 2, in both. -12 hides columns 1 and 2: only the common lines.
diff signMeaning
a / c / dAdd / change / delete
<Line from the first file
>Line from the second file
- / + (with -u)Line removed / added
@@ -1,4 +1,4 @@Which lines the change covers

في العمل

diff -u old new (unified format) is the standard. Git shows changes in the same format.

20.9

Apply changes with patch

patch reads the output of diff and makes the same changes to a file. Developers send small patches instead of whole files.

diff -Naur old_dir new_dir > diff_file.txt
diff -Naur file1.txt file2.txt > patchfile.txt
patch < patchfile.txt
patching file file1.txt

20.10

Change characters with tr

tr changes (translates) or deletes characters. It only reads stdin.

echo "lowercase letters" | tr a-z A-Z
LOWERCASE LETTERS
echo "lowercase letters" | tr [:lower:] A
AAAAAAAAA AAAAAAA
tr -d '\r' < dos_file > unix_file
echo "aaabbbccc" | tr -s ab
abccc
echo "secret text" | tr a-zA-Z n-za-mN-ZA-M
frperg grkg
  • -d deletes characters, -s squeezes repeats into one.
  • The last example is ROT13: each letter moves 13 places. Run it twice and you get the text back. It hides text but is not real encryption.

الأوامر في هذا الدرس

20.11

Edit text in a stream with sed

sed ("stream editor") changes text line by line with commands. The most used command is s/old/new/.

echo "front" | sed 's/front/back/'
back
echo "front" | sed 's_front_back_'
echo "front" | sed '1s/front/back/'
sed -n '1,5p' distros.txt
sed -n '/SUSE/p' distros.txt
sed -n '/SUSE/!p' distros.txt
sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/' distros.txt
sed -i 's/^#Port 22/Port 2222/' sshd_config
sed -f script.sed file.txt
AddressLines
nLine n ($ = last line)
/regex/Lines that match
a,bFrom a to b
first~stepEvery step-th line from first (1~2 = odd lines)
addr!All lines except these
CommandAction
pPrint the line (with -n: only these lines)
dDelete the line
s/re/new/flagsReplace; flag g = all matches in the line, i = ignore case
a, i, cAppend after, insert before, change the line
y/abc/xyz/Change characters, like tr
=Print the line number
  • \(…\) stores a part of the match; \1, \2 use it in the new text (back references). The example above turns MM/DD/YYYY into YYYY-MM-DD.
  • -i changes the file itself ("in place"); -i.bak keeps a copy.

انتبه

Test sed without -i first, look at the output, then add -i.

ملاحظة

Fans of sed also like awk (a small language for column data) and perl.

20.12

Check spelling with aspell

aspell checks the spelling of text files interactively. It knows HTML (-H).

aspell check foo.txt
aspell -H check foo.html

20.13

Summary of the chapter

sort, uniq, cut, paste, join, comm, diff, patch, tr, sed: small tools that together process any text.

1Which pipeline shows how many users use each login shell?
2How do you remove the Windows CR characters from a file?
3What does sed -n '/error/p' app.log do?

It prints only the lines that contain "error" (like grep error).

تدرّب

كل الأوامر في هذا الفصل