TRร„W๐ŸคŸ
TRร„W๐ŸคŸ

@thatstraw

32 Tweets 6 reads Oct 08, 2022
Basic Linux commands for text manipulation ๐Ÿงโ†“
Hello Linux folks, Today I'll be doing a quick, easy-to-follow thread on basic Linux text manipulation commands.
[1] Echo๐Ÿง
The echo command is used to display a line of text to the standard output(stdout).
[2] Printf๐Ÿง
The printf command is used to format and print data to the standard output.
[3] Cat
Cat is used to concatenate files and print their contents to standard output. In other words, it simply displays the contents of a file.
Did I tell you there is a modern version of cat (batcat) with syntax highlighting?๐Ÿค”
[4] Tac๐Ÿง
The tac command, like the cat command, concatenates and prints file contents in reverse order. The name suggests that this command is the inverse of the popular cat command.
[5] Paste๐Ÿง
The paste command, like the cat command, merges lines in a file into a single large single line.
[6] Head๐Ÿง
This command shows the first section of a file. Assume we have a lengthy file and you only want to see the first few lines. This is where the head command comes in; the head command, by default, displays the first 10 lines of a file.
However, you can also specify the number of lines that the command should display.
As previously stated, you can specify the number of lines to whatever you want; for example, let's say I only want to see the first 5ย lines. The -n in the example below represents the number of lines we want to display.
[7] Tail๐Ÿง
The tail command, like the head command, displays the last 10 lines of a file by default. However, you can always change it and specify the number of lines to display. I've chosen to show the last 5 lines in this case.
[8] Rev๐Ÿง
This command reverses lines characterwise.
[9] Sort๐Ÿง
The sort command is very useful for sorting files, and text lines.
Sorting by string numerical value is also possible with the sort command by using the -n option.
The sort command also has the capability of sorting in reverse order by using its -r option.
Finally, you can use the sort command to remove repeated lines, resulting in only unique lines. This is accomplished by using the sort command's -u option.
[10] Uniq (Unique)๐Ÿง
Another useful tool for text manipulation is the uniq (unique) command. It is used to remove or omit duplicates from a file, hence the name unique.
Uniq only removes adjacent duplicates, so we must sort our file before running it through uniq; otherwise, it will fail.
The preceding example is simply the equivalent of the 'sort -u' command; you can choose which one you prefer. In my case, I use both depending on which I thought about first.
[11] wc๐Ÿง
Word Count (wc) prints the number of newlines, words, and bytes in each file or supplied text. It displays the number of lines, words, and bytes respectively.
You can also specify what you want to display explicitly by using the l-, -w, or -c options, which will display the number of lines, words, or characters (bytes) respectively.
Let's try displaying only the number of words. Few ย free to experiment with other options.
[12] nl๐Ÿง
The nl (number lines) command is another option for determining the number of lines in a file. It displays the contents of a file as well as line numbers. This command is the equivalent of 'cat -n'ย  command.
[13] grep๐Ÿง
The grep command is one of the most common text processing commands you will use. It allows you to search files for characters that match a certain pattern.
Grep has many useful switches that I won't go over here, but if you want to learn more about this useful utility, check out the grep man pages.
[14] diff๐Ÿง
The diff command simply compares two text sources/text files and outputs their differences. It compares the files line by line to find the differences.
[15] vimdiff
Another excellent command for comparing files is vimdiff. Vim is launched on two to eight files using vimdiff. Each file has its own window.
The differences between the files are highlighted. This is an easy way to check for changes and move changes from one version of the same file to another.
$ vimdiff file1 file2
[16] Cut๐Ÿง
The cut command can be used to remove/extract bytes, characters, and fields from files. Various parameters are used to specify what part or parts of the file are to be removed or displayed.
Here is a quick example, let's say we want to display all the user names in the /etc/passwd file.
And that's a wrap!
If you enjoyed this thread:
1. Follow me (@xtremepentest) & give this a RT!
2. Join 1000+ subscribers inside of my weekly newsletter "Linux Weekly Catch Ups" and never miss any Linux, security, and networking content.
getrevue.co
3. Join my discord server and hang out with 1000+ other members.
discord.com

Loading suggestions...