{"id":388,"date":"2026-01-15T04:21:30","date_gmt":"2026-01-15T04:21:30","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=388"},"modified":"2026-01-28T04:36:34","modified_gmt":"2026-01-28T04:36:34","slug":"working-with-columns-and-fieldscut-paste-join","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=388","title":{"rendered":"Working with columns and fieldscut, paste, join"},"content":{"rendered":"\n<p>&#x2702;&#xfe0f; <code>cut<\/code> \u2013 Extract Columns (Fields) from a File<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What it does<\/h3>\n\n\n\n<p>Extracts <strong>specific columns\/fields<\/strong> from each line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cut -d \"delimiter\" -f field_list file\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Examples<\/h3>\n\n\n\n<p><strong>File: <code>emp.txt<\/code><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>101 Amit 45000 IT\n102 Neha 52000 HR\n103 Rahul 48000 IT\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Print <strong>ID and Name<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>cut -d \" \" -f 1,2 emp.txt\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CSV file (comma-separated)<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>cut -d \",\" -f 2 data.csv\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>By character position<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>cut -c 1-5 file.txt\ncat > stat.txt\nAndhra Pradesh\nArunachal Pradesh\nAssam\nBihar\nChhattisgarh\nctrl+z\n\ncut -b 1,2,3 stat.txt\ncut -b -3 stat.txt\ncut -c 2,3 stat.txt\n\n cat > b.txt\n01234:567;89\nabcd:drg;ij\nctrl+z\n\ncut -d \";\" -f1 b.txt  \/\/-f column name\ncut -d \":\" -f2 b.txt \nwho | cut -c 1-2\n\n<\/code><\/pre>\n\n\n\n<p>&#x26a0;&#xfe0f; Note: <code>cut<\/code> works best with <strong>single, consistent delimiters<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x1f4ce; <code>paste<\/code> \u2013 Combine Files Line by Line<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What it does<\/h3>\n\n\n\n<p>Merges lines of <strong>multiple files horizontally<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>paste file1 file2\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Examples<\/h3>\n\n\n\n<p><strong>Files<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>names.txt      salary.txt\nAmit           45000\nNeha           52000\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Combine side by side<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>paste names.txt salary.txt\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Amit    45000\nNeha    52000\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use comma as delimiter<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>paste -d \",\" names.txt salary.txt\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Merge all lines into one<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>paste -s names.txt\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>paste a.txt b.txt<br>paste -d &#8220;%&#8221; a.txt b.txt<br>paste -d &#8220;1,&#8221; a.txt b.txt c.txt<br>cat stat.txt | paste<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">&#x1f517; <code>join<\/code> \u2013 Join Two Files Using a Common Field<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What it does<\/h3>\n\n\n\n<p>Joins two files <strong>based on a common key (column)<\/strong><br>&#x27a1;&#xfe0f; Similar to <strong>SQL JOIN<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>join file1 file2\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p><strong>File: <code>emp_id.txt<\/code><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>101 Amit\n102 Neha\n103 Rahul\n<\/code><\/pre>\n\n\n\n<p><strong>File: <code>emp_dept.txt<\/code><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>101 IT\n102 HR\n103 IT\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Join on first column<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>join emp_id.txt emp_dept.txt\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>101 Amit IT\n102 Neha HR\n103 Rahul IT\n<\/code><\/pre>\n\n\n\n<p>&#x26a0;&#xfe0f; Important:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Files <strong>must be sorted<\/strong> on join field<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sort emp_id.txt &gt; a.txt\nsort emp_dept.txt &gt; b.txt\njoin a.txt b.txt\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Join on specific fields<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>join -1 1 -2 1 file1 file2\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x1f4ca; Quick Comparison (Very Important for Exams)<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Command<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>cut<\/code><\/td><td>Extract columns<\/td><\/tr><tr><td><code>paste<\/code><\/td><td>Combine files horizontally<\/td><\/tr><tr><td><code>join<\/code><\/td><td>Combine files using common field<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x1f9e0; Easy Memory Trick<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>cut<\/strong> \u2192 <em>take out<\/em> columns &#x2702;&#xfe0f;<\/li>\n\n\n\n<li><strong>paste<\/strong> \u2192 <em>stick together<\/em> files &#x1f4ce;<\/li>\n\n\n\n<li><strong>join<\/strong> \u2192 <em>connect<\/em> using key &#x1f517;<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>&#x2702;&#xfe0f; cut \u2013 Extract Columns (Fields) from a File What it does Extracts specific columns\/fields from each line. Syntax Examples File: emp.txt &#x26a0;&#xfe0f; Note: cut works best with single, consistent delimiters. &#x1f4ce; paste \u2013 Combine Files Line by Line What it does Merges lines of multiple files horizontally. Syntax Examples Files Output paste a.txt b.txtpaste [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-388","post","type-post","status-publish","format-standard","hentry","category-unix"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=388"}],"version-history":[{"count":1,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/388\/revisions"}],"predecessor-version":[{"id":389,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/388\/revisions\/389"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}