Grep Regex Example Linux

Reading Time: 4 minutes

What are Regular Expressions? Regular expressions are special characters which help search data, matching complex patterns. Regular expressions are shortened as ‘regexp’ or ‘regex’. Types of Regular expressions For ease of understanding let us learn the different types of Regex one by one. Basic Regular expressions Interval Regular expressions Extended regular expressions Summary Click here if the video is not accessible Basic Regular expressions Some of the commonly used commands with Regular expressions are tr, sed, vi and grep. Listed below are some of the basic Regex. Symbol Descriptions . replaces any character ^ matches start of string $ matches end of string * matches up zero or more times the preceding character \ Represent special characters () Groups regular expressions ? Matches up exactly one character Let’s see an example. Execute cat sample to see contents of an existing file Search for content containing letter ‘a’. ‘^’ matches the start of a string. Let’s search for content that STARTS with a Only lines that start with character are filtered. Lines which do not contain the character ‘a’ at the start are ignored. Let’s look into another example – Select only those lines that end with t using $ Interval Regular expressions These expressions tell us about the number of occurrences of a character in a string. They are Expression Description {n} Matches the preceding character appearing ‘n’ times exactly {n,m} Matches the preceding character appearing ‘n’ times but not more than m {n, } Matches the preceding character only when it appears ‘n’ times or more Example: Filter out all lines that contain character ‘p’ We want to check that the character ‘p’ appears exactly 2 times in a string one after the other. For this the syntax would be: cat sample | grep -E p\{2} Note: You need to add -E with these regular expressions. Extended regular expressions These regular expressions contain combinations of more than one expression. Some of them are: Expression Description \+ Matches one or more occurrence of the previous character \? Matches zero or one occurrence of the previous character Example: Searching for all characters ‘t’ Suppose we want to filter out lines where character ‘a’ precedes character ‘t’ We can use command like cat sample|grep “a\+t” Brace expansion The syntax for brace expansion is either a sequence or a comma separated list of items inside curly braces “{}”. The starting and ending items in a sequence are separated by two periods “..”. Some examples: In the above examples, the echo command creates strings using the brace expansion. Summary: Regular expressions are a set of characters used to check patterns in strings They are also called ‘regexp’ and ‘regex’ It is important to learn regular expressions for writing scripts Some basic regular expressions are: Symbol Descriptions . replaces any character ^ matches start of string $ matches end of string Some extended regular expressions are: Expression Description \+ Matches one or more occurrence of the previous character \? Matches zero or one occurrence of the previous character Some interval regular expressions are: Expression Description {n} Matches the preceding character appearing ‘n’ times exactly {n,m} Matches the preceding character appearing ‘n’ times but not more than m {n, } Matches the preceding character only when it appears ‘n’ times or more The brace expansion is used to generate strings. It helps in creating multiple strings out of one.  

Categorized as Linux Tagged

Input Redirection in Linux/Unix Examples

Reading Time: 2 minutes

What is Redirection?Redirection is a feature in Linux such that when executing a command, you can change the standard input/output devices. The basic workflow of any Linux command is that it takes an input and give an output. The standard input (stdin) device is the keyboard. The standard output (stdout) device is the screen. With redirection, the above standard input/output can be changed. In this tutorial, we will learn- Output Redirection Input redirection File Descriptors (FD) Error Redirection Why Error Redirection? Examples Click here if the video is not accessible Output Redirection The ‘>’ symbol is used for output (STDOUT) redirection. Example: ls -al > listings Here the output of command ls -al is re-directed to file “listings” instead of your screen. Note: Use the correct file name while redirecting command output to a file. If there is an existing file with the same name, the redirected command will delete the contents of that file and then it may be overwritten.” If you do not want a file to be overwritten but want to add more content to an existing file, then you should use ‘>>’ operator. You can redirect standard output, to not just files, but also devices! $ cat music.mp3 > /dev/audio The cat command reads the file music.mp3 and sends the output to /dev/audio which is the audio device. If the sound configurations in your PC are correct, this command will play the file music.mp3 Input redirectionThe ‘<' symbol is used for input(STDIN) redirection Example: The mail program in Linux can help you send emails from the Terminal. You can type the contents of the email using the standard device keyboard. But if you want to attach a File to email you can use the input re-direction operator in the following format. Mail -s "Subject" to-address < Filename This would attach the file with the email, and it would be sent to the recipient. The above examples were simple. Let's look at some advance re-direction techniques which make use of File Descriptors. File Descriptors (FD) In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an associated number called File Descriptor (FD). Your screen also has a File Descriptor. When a program is executed the output is sent to File Descriptor of the screen, and you see program output on your monitor. If the output is sent to File Descriptor of the printer, the program output would have been printed. Error Redirection Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard input, standard output, standard error. These files are always present whenever a program is run. As explained before a file descriptor, is associated with each of these files. File File Descriptor Standard Input STDIN 0 Standard Output STDOUT 1 Standard Error STDERR 2 By default, error stream is displayed on the screen. Error redirection is routing the errors to a file other than the screen. Why Error Redirection? Error re-direction is one of the very popular features of Unix/Linux. Frequent UNIX users will reckon that many commands give you massive amounts of errors. For instance, while searching for files, one typically gets permission denied errors. These errors usually do not help the person searching for a particular file. While executing shell scripts, you often do NOT want error messages cluttering up the normal program output. The solution is to re-direct the error messages to a file. Example 1 $ myprogram 2>errorsfile Above we are executing a program names myprogram. The file descriptor for standard error is 2. Using “2>” we re-direct the error output to a file named “errorfile” Thus, program output is not cluttered with errors. Example 2 Here is another example which uses find statement – find . -name ‘my*’ 2>error.log Using the “find” command, we are searching the “.” current directory for a file with “name” starting with “my” Example 3 Let’s see a more complex example, Server Administrators frequently, list directories and store both error and standard output into a file, which can be processed later. Here is the command. ls Documents ABC> dirlist 2>&1 Here, which writes the output from one file to the input of another file. 2>&1 means that STDERR redirects to the target of STDOUT (which is the file dirlist) We are redirecting error output to standard output which in turn is being re-directed to file dirlist. Hence, both the output is written to file dirlist Summary Each file in Linux has a corresponding File Descriptor associated with it The keyboard is the standard input device while your screen is the standard output device “>” is the output redirection operator. “>>” appends output to an existing file “<" is the input redirection operator ">&”re-directs output of one file to another. You can re-direct error using its corresponding File Descriptor 2.  

Categorized as Linux Tagged

Best Notepad Alternatives in 2021

Reading Time: 7 minutes

Notepad++ is open source code editor written in C++. It supports various programming languages running under the Microsoft Windows environment. But is has a few drawbacks. Here, is a curated list of top 10 alternatives which can replace Notepad++. This list includes commercial as well as open-source code editors (IDE) with popular features and latest download link. 1) Atom Atom is useful code editor tool preferred by programmers due to its simple interface compared to the other editors. Atom users can submit packages and them for the software. Platform: Windows, Mac, Linux Price: Free Features: Package Manager Integrated for Plugins support The feature of smart autocompletion Supports Command Palette Multiple panes Allow cross-platform editing Download link: 2) Emacs Emacs is a Unix based text editor tool which is used by programmers, engineers, students, and system administrators. It allows you to add, modify, delete, insert, words, letters, lines, and other units of text. Price: Free Platform: Mac, Windows, Linux Features: Complete built-in documentation Full Unicode support for many human scripts Highly customizable, using Emacs Lisp code. A packaging system for installing and downloading extensions Download link: 3) NetBeans NetBeans is an open-source code editor tool for developing with Java, PHP, C++, and other programming languages. With this editor, code analyzers, and converters. It allows you to upgrade your applications to use new Java 8 language constructs. Platforms: Mac Windows Linux Price: Free Features: Easy & Efficient Project Management Offers fast & Smart Code Editing Rapid User Interface Development Helps you to write bug-free code Download link: 4) jEdit jEdit, a code editor program which is written in Java. This open source tool supports hundreds of plugins and macros. It offers a large collection of plugins maintained by a worldwide developer team. Features: Built-in macro language & extensible plugin architecture Allows copy and paste with an unlimited number of clipboards You can download plugins with the help of the plugin manager. Register contents are saved across editing sessions. Allows auto indent, and syntax highlighting over 200 languages Price: Free Platform: Mac, Windows & Linux Download link: 5) Geany Geany is a text editor which uses GTK+ toolkit. It also has certain basic features of an integrated development environment. The tool supports many filetypes and has some nice features. Price: Free Platform: Mac, Windows, Linux Features: Allows you to add a note for applying the indent settings in the project preferences Navigating through the source code Allows popup menu on message window notebooks and sidebar Show status message on attempt to execute empty context action Download link: 6) TextMate TextMate is a versatile plain text editor for mac with unique and innovative features. The tool offers support for many programming languages, writing prose in structured formats such as blogging, running SQL queries, writing screenplays, etc. Price: Free Platform: MAC Features: Auto-Indent for Common Actions CSS-like Selectors to find the Scope of Actions and Settings Dynamic Outline for Working With Multiple Files Function Pop-up for Quick Overview and Navigation Run Shell Commands from Within a Document Visual Bookmarks to Jump Between Places in a File Download link: 7) VIM Vim is an advanced text editor open source tool which is also considered to be an IDE in its way. This tool allows managing your text editing activities with vim editors and UNIX System which can be used on-premise or online. Price: Free Platform: Linux Features: Extensive plugin support Powerful search and replace Integrates with many tools Macro recording and playbackSupport for hundreds of programming languages and file formats Download link: 8) Light Table Light Table is an IDE and text editor tool for software development. The tool offers fast feedback and allowing instant execution, debugging and access to documentation. Price: Free Platform: Mac, Windows, Linux Features: Inline EvaluationLight Table is a lightweight, clean, and sleek interface. Powerful editing and plugin manager The feature of println to keep track of critical values in your codeDownload link: 9) Brackets Brackets is a lightweight tool developed by Adobe. It is an open source text editor which is free to download. It allows you to a toggle between your source code and the browser view. Platform: Mac, Windows, Linux Price: Free Features: Quick Edit UIfeature puts context-specific code and tools inline Offers live preview, preprocessor support, and inline editors Pleasant looking UI Especially developed tool for macOS It comes with the inbuilt extension manager for fast & effective extension management.Download link: 10) Bluefish Bluefish a is a cross-platform editor is a speedy tool which can handle dozens of files simultaneously. The tool allows developers to conduct remote editing. This code editor tool offers many options to s programmers and web developers, to write websites, scripts, and programming code. Price: Free Platforms: Mac Windows Linux Features: Loads hundreds of files within seconds Auto-recovery of changes in modified documents after a crash, kill or shutdown. Project support feature helps you to work efficiently on multiple projects. Unlimited undo/redo functionality. Download link:  

Difference between Encryption and Decryption

Reading Time: 7 minutes

Before, we understand Encryption vs. Decryption let’s first understand- What is Cryptography? Cryptography is used to secure and protect data during communication. It is helpful to prevent unauthorized person or group of users from accessing any confidential data. Encryption and decryption are the two essential functionalities of cryptography. A message sent over the network is transformed into an unrecognizable encrypted message known as data encryption. At the receiving end, the received message is converted to its original form known as decryption. In this tutorial, you will learn What is Cryptography? What is meant By Encryption? What is meant by Decryption? Why use Encryption and Decryption? Types of Keys Difference between Encryption and Decryption What is meant By Encryption?Encryption is a process which transforms the original information into an unrecognizable form. This new form of the message is entirely different from the original message. That’s why a hacker is not able to read the data as senders use an encryption algorithm. Encryption is usually done using key algorithms. Data is encrypted to make it safe from stealing. However, many known companies also encrypt data to keep their trade secret from their competitors. Encryption Process What is meant by Decryption?Decryption is a process of converting encoded/encrypted data in a form that is readable and understood by a human or a computer. This method is performed by un-encrypting the text manually or by using keys used to encrypt the original data. Decryption process Why use Encryption and Decryption?Here, are important reasons for using encryption: Helps you to protect your confidential data such as passwords and login id Provides confidentiality of private information Helps you to ensure that that the document or file has not been altered Encryption process also prevents plagiarism and protects IP Helpful for network communication (like the internet) and where a hacker can easily access unencrypted data. It is an essential method as it helps you to securely protect data that you don’t want anyone else to have access. Types of KeysSymmetric Key:Symmetric-key encryption are algorithms which use the same cryptographic keys for both encryption of plaintext and decryption of ciphertext. Asymmetric Key:Asymmetric encryption uses 2 pairs of key for encryption. Public key is available to anyone while the secret key is only made available to the receiver of the message. This boots security. Public Key:Public key cryptography is an encryption system which is based on two pairs of keys. Public keys are used to encrypt messages for a receiver. Private Key:Private key may be part of a public/ private asymmetric key pair. It can be used in asymmetric encryption as you can use the same key to encrypt and decrypt data. Pre-Shared Key:In cryptography, a pre-shared key (PSK) is a shared secret which was earlier shared between the two parties using a secure channel before it is used. Difference between Encryption and Decryption table td{border-top:1px solid #ddd}@media only screen and (max-width:760px),(min-device-width:768px) and (max-device-width:1024px){table,c,tbody,th,td,tr{display:block}thead tr{position:absolute;top:-9999px;left:-9999px}tr{border:1px solid #ccc}td{border:none;border-bottom:1px solid #ddd;position:relative;padding-left:50%}td:before{position:absolute;top:6px;left:6px;width:45%;padding-right:10px}.table1 td:nth-of-type(1):before{content:”Parameter”}.table1 td:nth-of-type(2):before{content:”Encryption”}.table1 td:nth-of-type(3):before{content:”Decryption”}}Parameter Encryption Decryption What is It is a process of converting normal data into an unreadable form. It helps you to avoid any unauthorized access to data It is a method of converting the unreadable/coded data into its original form. Process Whenever the data is sent between two separate machines, it is encrypted automatically using a secret key. The receiver of the data automatically allows you to convert the data from the codes into its original form. Location of Conversion The person who is sending the data to the destination. The receiver receives the data and converts it. Example An employee is sending essential documents to his/her manager. The manager is receiving the essential documents from his/her employee. Use of Algorithm The same algorithm with the same key is used for the encryption-decryption process. The only single algorithm is used for encryption and decryption with a pair of keys where each use for encryption and decryption. Major function Transforming humanly understandable messages into an incomprehensible and obscure form that can not be interpreted. It is a conversion of an obscure message into an understandable form which is easy to understand by a human. SummaryCryptography is used to secure and protect data during communication. Encryption is a process which transforms the original information into an unrecognizable form. Decryption is a process of converting encoded/encrypted data in a form that is readable and understood by a human or a computer. Encryption method helps you to protect your confidential data such as passwords and login id. Public, Private, Pre-Shared and Symmetric are important keys used in cryptography. An employee is sending essential documents to his/her manager is an example of an encryption method. The manager is receiving the essential encrypted documents from his/her employee and decrypting it is an example of a decryption method. 

Linux Interview Questions

Reading Time: 21 minutes

1) What is Linux? Linux is an operating system based on UNIX and was first introduced by Linus Torvalds. It is based on the Linux Kernel and can run on different hardware platforms manufactured by Intel, MIPS, HP, IBM, SPARC, and Motorola. Another popular element in Linux is its mascot, a penguin figure named Tux. 2) What is the difference between UNIX and LINUX? Unix originally began as a propriety operating system from Bell Laboratories, which later on spawned into different commercial versions. On the other hand, Linux is free, open source and intended as a non-propriety operating system for the masses.

VI/VIM Text Editor Commands: Linux/Unix

Reading Time: 6 minutes

In this tutorial, you will learn- What is the VI editor? Command mode Insert mode Starting the vi editor vi Editing commands Moving within a file Saving and Closing the file What is the VI editor? The VI editor is the most popular and classic text editor in the Linux family. Below, are some reasons which make it a widely used editor – It is available in almost all Linux Distributions It works the same across different platforms and Distributions It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs Nowadays, there are advanced versions of the vi editor available, and the most popular one is VIM which is Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to learn vi because it is feature-rich and offers endless possibilities to edit a file. To work on VI editor, you need to understand its operation modes. They can be divided into two main parts. Click here if the video is not accessible Command mode: The vi editor opens in this mode, and it only understands commands In this mode, you can, move the cursor and cut, copy, paste the text This mode also saves the changes you have made to the file Commands are case sensitive. You should use the right letter case. Insert mode: This mode is for inserting text in the file. You can switch to the Insert mode from the command mode  by pressing ‘i’ on the keyboard Once you are in Insert mode, any key would be taken as an input for the file on which you are currently working. To return to the command mode and save the changes you have made you need to press the Esc key Starting the vi editor To launch the VI Editor -Open the Terminal (CLI) and type vi or &If you specify an existing file, then the editor would open it for you to edit. Else, you can create a new file. vi Editing commands Note: You should be in the “command mode” to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter-case. Keystrokes Action i Insert at cursor (goes into insert mode) a Write after cursor (goes into insert mode) A Write at the end of line (goes into insert mode) ESC Terminate insert mode u Undo last change U Undo all changes to the entire line o Open a new line (goes into insert mode) dd
3dd Delete line
Delete 3 lines. D Delete contents of line after the cursor C Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion. dw
4dw Delete word
Delete 4 words cw Change word x Delete character at the cursor r Replace character R Overwrite characters from cursor onward s Substitute one character under cursor continue to insert S Substitute entire line and begin to insert at the beginning of the line ~ Change case of individual character Make sure you press the right command otherwise you will end up making undesirable changes to the file. You can also enter the insert mode by pressing a, A, o, as required. Moving within a file You need to be in the command mode to move within a file. The default keys for navigation are mentioned below else; You can also use the arrow keys on the keyboard. Keystroke Use k Move cursor up j Move cursor down h Move cursor left l Move cursor right Saving and Closing the file You should be in the command mode to exit the editor and save changes to the file. Keystroke Use Shift+zz Save the file and quit :w Save the file but keep it open :q Quit without saving :wq Save the file and quit Summary: The vi editor is the most popular and commonly used Linux text editor It is usually available in all Linux Distributions. It works in two modes, Command and Insert Command mode takes the user commands, and the Insert mode is for editing text You should know the commands to work on your file easily Learning to use this editor can benefit you in creating scripts and editing files.  

Categorized as Linux Tagged