Библиотека сайта или "Мой Linux Documentation Project"
Also see: Also see tac, and cat over in this section, the Section called Text Viewing Tools, as they can perform text manipulation too
Sorting text with no options the sort is alphabetical. Can be run on text files to sort them alphabetically (note it also concatenates files), can also be used with a pipe '|' to sort the output of a command.
cat shoppinglist.txt | sort
sort -r shoppinglist.txt
Advanced sort commands:
sort is a powerful utility, here are some of the more hard to learn (and lesser used) commands. Use the -t option to use a particular symbol as the separator then use the -k option to specify which column you would like to sort by, where column 1 is the first column before the separator. Also use the -g option if numeric sorting is not working correctly (without the -g option sort just looks at the first digit of the number). Here is a complex example:
This will sort the “/etc/passwd” file, using the colon ':' as the separator. It will sort via the 4th column (GID section, in the file) and then sort within that sort using the first (name) to resolve any ties. The -g is there so it sorts via full numbers, otherwise it will have 4000 before 50 (it will just look at the first digit...).
join file1 file2
Prints selected parts of lines (of a text file), or, in other words, removes certain sections of a line. You may wish to remove things according to tabs or commas, or anything else you can think of...
Options for cut:
cut -d ':' (and probably some more options here) /etc/passwd
-f --- this option works with the text by columns, separated according to the delimiter. For example if your file had lines like “result,somethingelse,somethingelse” and you only wanted result you would use:
cut -d ',' -f 1 /etc/passwd
cut -d ':' -f 1,7 /etc/passwd
cut -c 1-50 file1.txt
cut -5, 20-, 8 file2.txt
aspell -c FILE.txt
cat fileName.txt | tr '[A-Z]' '[a-z]' > newFileName.txt
cat fileName.txt | tr '[a-z]' '[A-Z]' > newFileName.txt
chcase (a perl script) can be found at the chcase homepage.
(format) a simple text formatter. Use fmt with the -u option to output text with "uniform spacing", where the space between words is reduced to one space character and the space between sentences is reduced to two space characters.
fmt -u myessay.txt
Puts lines from two files together, either lines of each file side by side (normally separated by a tab-stop but you can have any symbols(s) you like...) or it can have words from each file (the first file then the second file) side by side.
paste file1.txt file2.txt
paste --serial file1.txt file2.txt
This command is very simple to understand if you make yourself an example: Its much easier if you create an example for yourself. With just a couple of lines, I used "first line first file" and "first line second file" et cetera for a quick example.
uniq -cd phone_list.txt
cat some_file | tr '3' '5' > new_file
This will run the cat program on some file, the output of this command will be sent to the tr command, tr will replace all the instances of 3 with 5, like a search and replace. You can also do other things such as:
cat some_file | tr '[A-Z]' '[a-z]' > new_file
Alternatives: You can also do a search and replace with a one line Perl command, read about it at the end of this section.
Also try cat -n (number all lines) or cat -b (number all non-blank lines). For more info on cat check under this section: the Section called Text Viewing Tools
There are two ways you can use nl:
grep some_string some_file | nl
- Perl search and replace text
To search and replace text in a file is to use the following one-line Perl command:
$ perl -pi -e "s/oldstring/newstring/g;" filespec [RET]
In this example, “oldstring” is the string to search, “newstring” is the string to replace it with, and “filespec” is the name of the file or files to work on. You can use this for more than one file.
$ perl -pi -e "s/helpless/helpful/g;" * [RET]
If these tools are too primitive: If these text tools are too simple for your purposes then you are probably looking at doing some programming or scripting.
If you would like more information on bash scripting then please see the advanced bash scripting guide, authored by Mendel Cooper.
sed and awk are traditional UNIX system tools for working with text, this guide does not provide an explanation of them. sed works on a line-by-line basis performing substitution and awk can perform a similar task or assist by working on a file and printing out certain information (its a programming language).
You will normally find them installed on your GNU/Linux system and will find many tutorials all over the internet, feel free to look them up if you ever have to perform many similar operations on a text file.
This information has been taken from the Linux Cookbook (without editing). See  in the Bibliography for further information.
Только зарегистрированные пользователи могут оценивать и комментировать статьи.