هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
المهارات في هذا الفصل
- فرز النصوص وقصّها ومقارنتها وتعديلها بالأدوات الكلاسيكية
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.
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| Option | What it does |
|---|---|
-A | Show all: tabs as ^I, line ends as $ |
-n | Number the lines |
-s | Squeeze 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.
Sort lines with sort
sort sorts lines. It can sort by number, by a field (column), and merge sorted files.
| Option | What it does |
|---|---|
-b | Ignore spaces at the start |
-f | Ignore case |
-n | Sort by number |
-h | Sort human sizes (2K, 1G) |
-r | Reverse |
-k field | Sort by a field (-k 3, -k 3.7 = from character 7 of field 3) |
-t char | The field separator (default: spaces/tabs) |
-m | Merge sorted files |
-o file | Write to a file (can be the input file) |
-u | Remove 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,1sorts only by field 1.-k 1would use field 1 to the end of the line.
Report repeated lines with uniq
uniq removes or reports repeated lines. The input must be sorted.
Cut columns with cut
cut takes parts of each line: characters (-c) or fields (-f) separated by a character (-d, default tab).
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.
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.
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.txtcommprints 3 columns: only in file 1, only in file 2, in both.-12hides columns 1 and 2: only the common lines.
| diff sign | Meaning |
|---|---|
a / c / d | Add / 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.
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.txtChange 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-ddeletes characters,-ssqueezes 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.
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| Address | Lines |
|---|---|
n | Line n ($ = last line) |
/regex/ | Lines that match |
a,b | From a to b |
first~step | Every step-th line from first (1~2 = odd lines) |
addr! | All lines except these |
| Command | Action |
|---|---|
p | Print the line (with -n: only these lines) |
d | Delete the line |
s/re/new/flags | Replace; flag g = all matches in the line, i = ignore case |
a, i, c | Append after, insert before, change the line |
y/abc/xyz/ | Change characters, like tr |
= | Print the line number |
\(…\)stores a part of the match;\1,\2use it in the new text (back references). The example above turnsMM/DD/YYYYintoYYYY-MM-DD.-ichanges the file itself ("in place");-i.bakkeeps a copy.
انتبه
Test sed without -i first, look at the output, then add -i.
Check spelling with aspell
aspell checks the spelling of text files interactively. It knows HTML (-H).
aspell check foo.txt
aspell -H check foo.htmlSummary 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?
tr -d '\r' < in.txt > out.txt (or dos2unix).
3What does sed -n '/error/p' app.log do?
It prints only the lines that contain "error" (like grep error).