<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shamil</title>
    <description>The latest articles on Forem by Shamil (@shamil).</description>
    <link>https://forem.com/shamil</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F9825%2F32d562a3-9f99-4c1c-9428-38e88123fb33.jpeg</url>
      <title>Forem: Shamil</title>
      <link>https://forem.com/shamil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shamil"/>
    <language>en</language>
    <item>
      <title>Processing Text with Linux Shell - Part 2 </title>
      <dc:creator>Shamil</dc:creator>
      <pubDate>Sun, 29 Jul 2018 19:39:20 +0000</pubDate>
      <link>https://forem.com/shamil/processing-text-with-linux-shell---part-2--ogo</link>
      <guid>https://forem.com/shamil/processing-text-with-linux-shell---part-2--ogo</guid>
      <description>&lt;h1&gt;
  
  
  &lt;code&gt;grep&lt;/code&gt; - searching the haystack made easy
&lt;/h1&gt;

&lt;p&gt;When you need to search a piece of text from a very large source, &lt;code&gt;grep&lt;/code&gt; command is the answer. &lt;code&gt;grep&lt;/code&gt; accepts strings, regular expressions and it can produce output in various formats.&lt;/p&gt;

&lt;p&gt;The most basic syntax to search a piece of text in a file is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"pattern"&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we need to search multiple files for a pattern, we can do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"pattern"&lt;/span&gt; file1 file2 file3 ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;grep&lt;/code&gt; can also be used with standard input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"linux is awesome"&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;linux
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If no file or directory is specified, &lt;code&gt;grep&lt;/code&gt; reads the &lt;code&gt;STDIN&lt;/code&gt; for input.&lt;/p&gt;

&lt;h3&gt;
  
  
  # Highlight the matched text
&lt;/h3&gt;

&lt;p&gt;One major feature of grep is it's ability to highlight the matched string in its output. There are 3 color options: &lt;code&gt;auto&lt;/code&gt;, &lt;code&gt;always&lt;/code&gt; and &lt;code&gt;never&lt;/code&gt;. To use the color options, we can pass an additional &lt;code&gt;--color&lt;/code&gt; flag to instruct which option to use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"pattern"&lt;/span&gt; file_name &lt;span class="nt"&gt;--color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;always
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we can already guess, with &lt;code&gt;--color=always&lt;/code&gt; option it will always color the output matched string. With &lt;code&gt;--color=auto&lt;/code&gt;, &lt;code&gt;grep&lt;/code&gt; displays color in the output only if the output is not piped to a command or redirected to any file. The last option, &lt;code&gt;--color=never&lt;/code&gt; will turn off the coloring.&lt;/p&gt;

&lt;p&gt;If you want to turn on coloring for every &lt;code&gt;grep&lt;/code&gt; operation, but feeling lazy to type &lt;code&gt;--color=always&lt;/code&gt; with each command (which would be me), you could add &lt;code&gt;GREP_OPTIONS&lt;/code&gt; to your environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;GREP_OPTIONS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'--color=always'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will turn on coloring for all &lt;code&gt;grep&lt;/code&gt; commands.&lt;/p&gt;

&lt;h3&gt;
  
  
  # Limit what to output
&lt;/h3&gt;

&lt;p&gt;Normally, &lt;code&gt;grep&lt;/code&gt; only outputs the line that contains the matched pattern. But there are circumstances where we might want to see the contents of the entire file with the matched pattern highlighted. We can do the following for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;--color&lt;/span&gt; &lt;span class="s1"&gt;'pattern\|'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will output all the lines containing the pattern as well as the lines that have an end, i.e the complete contents of the file with only the matched part colored. Of course, we can pass multile patterns to be matched.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;--color&lt;/span&gt; &lt;span class="s1"&gt;'pattern_one\|pattern_two\|'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;\&lt;/code&gt; before &lt;code&gt;|&lt;/code&gt; . This is because &lt;code&gt;grep&lt;/code&gt; only interprets some of the special characters. However we can also write commands without &lt;code&gt;\&lt;/code&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="nt"&gt;--color&lt;/span&gt; &lt;span class="s1"&gt;'pattern_one|pattern_two|'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-E&lt;/code&gt; option instructs &lt;code&gt;grep&lt;/code&gt; to use full set of regular expressions. However, there is a better way of writing such patterns containing regular expressions . We can use &lt;code&gt;egrep&lt;/code&gt;, which is an extended version of &lt;code&gt;grep&lt;/code&gt; that supports extended regular expressions out of the box. Therefore, we can rewrite the above commands like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;egrep &lt;span class="nt"&gt;--color&lt;/span&gt; &lt;span class="s1"&gt;'pattern_one|pattern_two|'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But what if we neither need the complete line that contains the matching text nor the complete contents, rather just the matching content? &lt;/p&gt;

&lt;p&gt;For example, if we are searching for all the email addresses in a file, we do not need the line that contains the email, rather we just need all the emails that appears in the file.&lt;/p&gt;

&lt;p&gt;Well, we can use the &lt;code&gt;-o&lt;/code&gt; option, which stands for &lt;em&gt;only matching&lt;/em&gt;, that limits the output to only the matched text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;egrep &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="s2"&gt;[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;[A-Za-z]{2,6}&lt;/span&gt;&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Neat, isn't it? &lt;/p&gt;

&lt;p&gt;What if we need to see all the lines &lt;em&gt;except&lt;/em&gt; the ones where the given pattern appears?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"pattern"&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-v&lt;/code&gt; option inverts the results and thus the above command will output all the contents except the lines containing the matched pattern.&lt;/p&gt;

&lt;h3&gt;
  
  
  # Count the appearances
&lt;/h3&gt;

&lt;p&gt;If we need to get a count of how many times a pattern appears in a file, we can use the &lt;code&gt;-c&lt;/code&gt; flag. From the above email pattern example, if we need to see how many email addresses are there in a file, we could do the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;egrep &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="s2"&gt;[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;[A-Za-z]{2,6}&lt;/span&gt;&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But wait, there is a gotcha. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-c&lt;/code&gt; counts only the number of lines that contains the pattern, not the actual number of matched pattern. So if there are multiple matches in a single line, it will count only once!&lt;/p&gt;

&lt;p&gt;We can overcome this with a little tweak. &lt;/p&gt;

&lt;p&gt;Remember how we printed only the matching portions with the &lt;code&gt;-o&lt;/code&gt; flag above. If pipe &lt;code&gt;wc -l&lt;/code&gt; with it,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;egrep &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="s2"&gt;[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="s2"&gt;[A-Za-z]{2,6}&lt;/span&gt;&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; file_name | &lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;it will give us the exact number of times a particular pattern appears in a file.&lt;/p&gt;

&lt;h3&gt;
  
  
  # Extract additional information
&lt;/h3&gt;

&lt;p&gt;If we want to print the line numbers where the searched pattern appears, we can use the &lt;code&gt;-n&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; &lt;span class="nt"&gt;--color&lt;/span&gt; &lt;span class="s2"&gt;"pattern"&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-n&lt;/code&gt; flag will also print the name of the file, in case we are searching in multiple files.&lt;/p&gt;

&lt;p&gt;If we want to see the offset where the pattern starts, we can use the &lt;code&gt;-b&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"linux is the answer"&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-b&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="s2"&gt;"the"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is print &lt;code&gt;9:the&lt;/code&gt; as the searched word &lt;em&gt;"the"&lt;/em&gt; starts at the 9th position of the line. Note that that &lt;code&gt;-b&lt;/code&gt; flag is always used with the &lt;code&gt;-o&lt;/code&gt; flag.&lt;/p&gt;

&lt;h3&gt;
  
  
  # Search within directories
&lt;/h3&gt;

&lt;p&gt;To see which files contain a particular pattern , we can use the &lt;code&gt;-l&lt;/code&gt; flag.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; &lt;span class="s2"&gt;"pattern"&lt;/span&gt; file1 file2 file3 ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will output only the file names contains the given pattern. If we want to see the name of the files that does not contain the files, we can use &lt;code&gt;-L&lt;/code&gt; flag (note the case-sensitive flags). This basically inverts results we get with &lt;code&gt;-l&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;Well, so far we have only seen how to search in a single file or a set of file which are specified in the command. However, we can also search all the files in a complete directory and it's subdirectories recursively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s2"&gt;"pattern"&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;.&lt;/code&gt; specifies the current directory. The &lt;code&gt;-r&lt;/code&gt; flag implies that the search should be done recursively in all subdirectories. This search is case-sensitive. &lt;/p&gt;

&lt;p&gt;If we want to search all occurances of the pattern, we can append &lt;code&gt;-i&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-ir&lt;/span&gt; &lt;span class="s2"&gt;"pattern"&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will output all matching patterns irrespective of the case.&lt;/p&gt;

&lt;h3&gt;
  
  
  # The pattern file
&lt;/h3&gt;

&lt;p&gt;If we have too many patterns, strings etc that we want to search, piping them with &lt;code&gt;|&lt;/code&gt; is very tedious. In such scenarios, we can use a pattern file, that contains all the strings/patterns we want to search, &lt;em&gt;each in a new line&lt;/em&gt;. Then we can use that pattern file in the &lt;code&gt;grep&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-ir&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; pattern_file
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will print all the matching strings from the pattern file.&lt;/p&gt;

&lt;p&gt;ヽ(´▽`)/&lt;/p&gt;

&lt;p&gt;(You can find Part 1 of this series &lt;a href="https://dev.to/shamilchoudhury/processing-text-with-linux-shell---part-1-4ajd"&gt;here&lt;/a&gt; )&lt;/p&gt;

</description>
      <category>bash</category>
      <category>linux</category>
      <category>shell</category>
      <category>textprocessing</category>
    </item>
    <item>
      <title>Processing Text with Linux Shell - Part 1</title>
      <dc:creator>Shamil</dc:creator>
      <pubDate>Fri, 27 Jul 2018 20:50:46 +0000</pubDate>
      <link>https://forem.com/shamil/processing-text-with-linux-shell---part-1-4ajd</link>
      <guid>https://forem.com/shamil/processing-text-with-linux-shell---part-1-4ajd</guid>
      <description>&lt;h1&gt;
  
  
  Into the world of &lt;code&gt;sed&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;If you are using any *&lt;em&gt;nix&lt;/em&gt; systems on a daily basis, chances are you are already familiar with, or at least you have heard about the &lt;code&gt;sed&lt;/code&gt; command. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;sed&lt;/code&gt; , short for &lt;code&gt;Stream Editor&lt;/code&gt;, is a text transformation tool that comes bundled with every unix system. What makes &lt;code&gt;sed&lt;/code&gt; distinguishable from other text editors is the speed at which the text manipulation is performed. &lt;code&gt;sed&lt;/code&gt; only makes one pass over the input text, therefore making the processing quite faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  # Replace those ugly text
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sed&lt;/code&gt; is a very powerful tool to replace a piece of text with another. The text can be matched using regular expressions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/text_to_be_replaced/replacement_text/'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, this will only print the substitued text in the console, but won't change the same in the file itself. If we want to save the changes to the file, we can use the &lt;code&gt;-i&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/text_to_be_replaced/replacement_text/'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This above replaces only the first occurance of the given pattern in each line. So if we want to replace every occurence of the pattern, we can append the &lt;code&gt;g&lt;/code&gt; parameter to the end.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/text_to_be_replaced/replacement_text/g'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that the delimiter character &lt;code&gt;/&lt;/code&gt; we used in the above commands is not fixed, we can use almost any delimiter character in &lt;code&gt;sed&lt;/code&gt;. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s:text_to_be_replaced:replacement_text:g'&lt;/span&gt; file_name


&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s|text_to_be_replaced|replacement_text|g'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay, but what if the delimiter character is itself a part of the pattern to be replaced? ¿ⓧ_ⓧﮌ&lt;/p&gt;

&lt;p&gt;Well, we can escape that character with a backslash. For example, to replace the word &lt;code&gt;following:&lt;/code&gt; with &lt;code&gt;below -&lt;/code&gt; , we can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s:following\::below - :'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice the use of &lt;code&gt;\:&lt;/code&gt; before the delimiter &lt;code&gt;:&lt;/code&gt; that separates the pattern and it's replacement. &lt;/p&gt;

&lt;h3&gt;
  
  
  # Delete that scrap
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sed&lt;/code&gt; also allows us to delete lines from a file. The &lt;code&gt;d&lt;/code&gt; option is used to indicate a delete operation. The generic syntax to delete line is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'Nd'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;N&lt;/code&gt; is the line number that we want to delete. If we want to delete the 10th line from a file, &lt;code&gt;N&lt;/code&gt; would be 10.&lt;/p&gt;

&lt;p&gt;One most common use of this command is deleting all blank lines in a file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'/^$/d'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above will delete all the blank lines in the file. The regular expression &lt;code&gt;^$&lt;/code&gt; marks an empty line and the &lt;code&gt;d&lt;/code&gt; option specifies that the line should be deleted.&lt;/p&gt;

&lt;p&gt;That's not it. We can also specify a range of lines that should be deleted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'m,nd'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above command will delete all the lines starting from &lt;code&gt;m&lt;/code&gt;th upto &lt;code&gt;n&lt;/code&gt;th.&lt;/p&gt;

&lt;h3&gt;
  
  
  # Pipelining is important
&lt;/h3&gt;

&lt;p&gt;Now what about pipelining multiple &lt;code&gt;sed&lt;/code&gt; commands? &lt;/p&gt;

&lt;p&gt;We can pipeline as many &lt;code&gt;sed&lt;/code&gt; as we wish and they would be processed in that order. Consider the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo &lt;/span&gt;Linux | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/L/l/'&lt;/span&gt; | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/n/N/'&lt;/span&gt; | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/l/L/'&lt;/span&gt; | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/x/X/'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will output &lt;code&gt;LiNuX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally let's take a look at how we can use variables within &lt;code&gt;sed&lt;/code&gt; command. So far we have used &lt;code&gt;' '&lt;/code&gt; (single quote) in our commands. However we can aslo use &lt;code&gt;" "&lt;/code&gt; when we need to use an expression in our command. Take a look at the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;hello

&lt;span class="nb"&gt;echo &lt;/span&gt;hello shamil | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s2"&gt;"s/&lt;/span&gt;&lt;span class="nv"&gt;$greet&lt;/span&gt;&lt;span class="s2"&gt;/hi"&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will replace evaluate the value of &lt;code&gt;$greet&lt;/code&gt; and and replace &lt;code&gt;hello&lt;/code&gt; with &lt;code&gt;hi&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  # Better safe than sorry
&lt;/h3&gt;

&lt;p&gt;When using &lt;code&gt;-i&lt;/code&gt; in the &lt;code&gt;sed&lt;/code&gt; command, we need to be careful, as it replaces the actual content in the file. (Trust me, I have done this many times)&lt;/p&gt;

&lt;p&gt;Therefore, it is a good practice to first use this command without &lt;code&gt;-i&lt;/code&gt; flag and check if the replacements are correct. However, if the file contents are too long to be checked like that, you can use the following command to create a backup copy of the same and then modifying the content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt;.bak &lt;span class="s1"&gt;'12,30d'&lt;/span&gt; file_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will delete all lines from 12 to 30, but most importantly it will create a &lt;code&gt;file_name.bak&lt;/code&gt; in the same directory &lt;em&gt;before modifying the actual file&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Who knows, this might just end up saving your job (◠﹏◠)&lt;/p&gt;

&lt;p&gt;(EDIT: See &lt;a href="https://dev.to/moopet/comment/4dna"&gt;this comment&lt;/a&gt; for more info on &lt;code&gt;-i&lt;/code&gt; usages)&lt;/p&gt;

</description>
      <category>linux</category>
      <category>shell</category>
      <category>textprocessing</category>
    </item>
  </channel>
</rss>
