Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Command line quick tips: Cutting content out of files

#1
Command line quick tips: Cutting content out of files

<div><p>The Fedora distribution is a full featured operating system with an excellent graphical desktop environment. A user can point and click their way through just about any typical task easily. All of this wonderful ease of use masks the details of a powerful command line under the hood. This article is part of a series that shows you some common command line utilities. So let’s drop into the shell, and have a look at <strong>cut</strong>.</p>
<p>Often when you work in the command line, you are working with text files. Sometimes these files may be quite long. Reading them in their entirety, while feasible, can be time consuming and prone to errors. In this installment you’ll learn how to extract content from text files, and get the information you want from them.</p>
<p> <span id="more-22824"></span> </p>
<p>It’s important to recognize that there are many ways to accomplish similar command line tasks in Fedora. The Fedora repositories include entire language systems for parsing and working with text, as an example. Also, there are multiple command line utilities available for just about any purpose conceivable in the shell. This article will only focus on using a few of those utility choices, to extract some information from a file and present it in a readable format.</p>
<h2>Making the cut</h2>
<p>To illustrate this example use a standard sizable file on the system like <em>/etc/passwd</em>.&nbsp;As seen in a prior article in this series, you can execute the&nbsp;<em>cat</em> command to view an entire file: </p>
<pre class="wp-block-preformatted">$ <strong>cat /etc/passwd</strong>
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
...</pre>
<p>This file contains information on all accounts present on the system. It has a specific format:
</p>
<pre class="wp-block-preformatted"><em>name:password:user-id:group-id:comment:home-directoryConfusedhell</em></pre>
<p>Imagine that you want to simply have a list of all the account names on the system. If you could only cut out the&nbsp;<em>name</em>&nbsp;value from each line. This is where the&nbsp;<em>cut</em> command comes in handy! This command treats any input one line at a time, and extracts a specific part of the line. </p>
<p>The&nbsp;<em>cut</em> command provides options for selecting parts of a line differently, and in this example two of them are needed, <em>-d</em> which is an option to specify a delimiter type to use, and <em>-f</em> which is an option to specify which field of the line to cut. The&nbsp;<em>-d</em> option lets you declare the&nbsp;<em>delimiter</em> that separates values in a line. In this case a colon (Smile is used to separate values. The&nbsp;<em>-f</em> option lets you choose which field value or values to extract. So for this example the command entered would be: </p>
<pre class="wp-block-preformatted">$ <strong>cut -d: -f1 /etc/passwd</strong><br />root<br />bin<br />daemon<br />adm<br />...</pre>
<p>That’s great, it worked! But you get the printout to the standard output, which in a terminal session at least means the screen. What if you needed the information for another task to be done later? It would be really nice if there was a way to put the output of the <em>cut</em> command into a text file to save it. There is an easy builtin shell function for such a task, the redirect function (<em>&gt;</em>).</p>
<pre class="wp-block-preformatted">$ <strong>cut -d: -f1 /etc/passwd &gt; names.txt</strong></pre>
<p>This will place the output of cut into a file called <em>names.txt</em> and you can check the contents with <em>cat:</em></p>
<pre class="wp-block-preformatted">$ <strong>cat names.txt</strong><br />root<br />bin<br />daemon<br />adm<br />...</pre>
<p>With two commands and one shell function, it was easy to identify using <em>cat</em>, extract using <em>cut</em>, and redirect the extracted information from one file, saving it to another file for later use.</p>
<hr class="wp-block-separator" />
<p><em>Photo by </em><a href="https://unsplash.com/photos/tA5eSY_hay8?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><em>Joel Mbugua</em></a><em> on </em><a href="https://unsplash.com/search/photos/command-line?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><em>Unsplash</em></a><em>.</em></p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016