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.  

Published
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.  

Published
Categorized as Linux Tagged