[Tut] 5 Easy Ways to Edit a Text File From Command Line (Windows) - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] 5 Easy Ways to Edit a Text File From Command Line (Windows) (/thread-100832.html) |
[Tut] 5 Easy Ways to Edit a Text File From Command Line (Windows) - xSicKxBot - 03-05-2023 5 Easy Ways to Edit a Text File From Command Line (Windows) <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{"align":"left","id":"1182560","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","legendonly":"","readonly":"","score":"5","starsonly":"","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}'> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </p></div> <h2>Problem Formulation</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="611" height="629" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-33.png" alt="" class="wp-image-1182658" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-33.png 611w, https://blog.finxter.com/wp-content/uploads/2023/03/image-33-291x300.png 291w" sizes="(max-width: 611px) 100vw, 611px" /></figure> </div> <p>Given is a text file, say <code>my_file.txt</code>. How to modify its content in your Windows command line working directory?</p> <p>I’ll start with the most direct method to solve this problem in 90% of cases and give a more “pure” in-terminal method afterward.</p> <h2>Method 1: Using Notepad</h2> <p>The easiest way to edit a text file in the command line (CMD) on your Windows machine is to run the command <code>notepad.exe my_text_file.txt</code>, or simply <code>notepad my_text_file.txt</code>, in your cmd to open the text file with the visual editor Notepad. </p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">notepad.exe my_file.txt</pre> <p>You can also skip the <code>.exe</code> prefix in most cases:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">notepad my_text_file.txt</pre> <p>Now, you may ask:</p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Is Notepad preinstalled in any Windows installation?</strong> The answer is: <strong>yes</strong>! Notepad is a generic text editor to create, open, and read plaintext files and it’s included with all Windows versions.</p> <p>Here’s how that looks on my Win 10 machine:</p> <div class="wp-block-image"> <figure class="aligncenter size-large"><img decoding="async" loading="lazy" width="1024" height="368" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-28-1024x368.png" alt="" class="wp-image-1182587" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-28-1024x368.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/03/image-28-300x108.png 300w, https://blog.finxter.com/wp-content/uploads/2023/03/image-28-768x276.png 768w, https://blog.finxter.com/wp-content/uploads/2023/03/image-28.png 1116w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure> </div> <p>When I type in the command <code>notepad.exe my_text_file.txt</code>, CMD starts the Notepad visual editor in a new window. </p> <p>I can then edit the file and hit <code>CTRL + S</code> to save the new contents.</p> <p>But what if you cannot open a text editor—e.g. if you’re logged into a remote server via SSH?</p> <h2>Method 2: Pure CMD Approach</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="684" height="453" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-32.png" alt="" class="wp-image-1182656" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-32.png 684w, https://blog.finxter.com/wp-content/uploads/2023/03/image-32-300x199.png 300w" sizes="(max-width: 684px) 100vw, 684px" /></figure> </div> <p>If you cannot open Notepad or other visual editors for some reason, a simple way to overwrite a text file with built-in Windows command line tools is the following:</p> <ul> <li>Run the command <code>echo 'your new content' > my_file.txt</code> to print the new content using <code>echo</code> and pipe the output into the text file <code>my_text_file.txt</code> using <code>></code>.</li> <li>Check the new content using the command <code>type my_text_file.txt</code>.</li> </ul> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">C:\Users\xcent\Desktop>echo 'hello world' > my_file.txt C:\Users\xcent\Desktop>type my_file.txt 'hello world'</pre> <p>Here’s what this looks like on my Windows machine, where I changed <code>my_file.txt</code> to contain the text <code>'hello world!'</code>:</p> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="721" height="107" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-29.png" alt="" class="wp-image-1182600" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-29.png 721w, https://blog.finxter.com/wp-content/uploads/2023/03/image-29-300x45.png 300w" sizes="(max-width: 721px) 100vw, 721px" /></figure> </div> <p>This is a simple and straightforward approach to small changes. However, if you have a large file and you just want to edit some minor details, this is not the best way. </p> <h2>Method 3: Change File Purely In CMD (Copy Con)</h2> <p>If you need a full-fledged solution to edit potentially large files in your Windows CMD, use this method! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <p class="has-global-color-8-background-color has-background">To create a new file in Windows command prompt, enter <code>copy con</code> followed by the target file name (<code>copy con my_file.txt</code>). Then enter the text you want to put in the file. To end and save the file, press <code>Ctrl+Z</code> then <code>Enter</code> or <code>F6</code> then <code>Enter</code>.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">copy con my_file.txt</pre> <p>How this looks on my Win machine:</p> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="833" height="475" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-30.png" alt="" class="wp-image-1182626" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-30.png 833w, https://blog.finxter.com/wp-content/uploads/2023/03/image-30-300x171.png 300w, https://blog.finxter.com/wp-content/uploads/2023/03/image-30-768x438.png 768w" sizes="(max-width: 833px) 100vw, 833px" /></figure> </div> <p>A couple of notes:</p> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Info</strong>: <em>To edit an existing file, display the text by using the <code>type</code> command followed by the file name. Then copy and paste the text into the <code>copy con</code> command to make changes. Be careful not to make any typos, or you’ll have to start over again. Backspace works if you catch the mistake before pressing Enter. Note that this method may not work in PowerShell or other command line interfaces that don’t support this feature.</em></p> <h2>Method 4: If you SSH’d to a Unix Machine</h2> <p>Of course, if you have logged in a Unix-based machine, you don’t need to install any editor because it comes with powerful integrated editors such as vim or emacs. </p> <p>One of the following three commands should open your file in a terminal-based editing mode:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">vim my_text_file.txt vi my_text_file.txt emacs my_text_file.txt</pre> <p>You can learn more about <a rel="noreferrer noopener" href="https://www.vim.org/" data-type="URL" data-id="https://www.vim.org/" target="_blank">Vim here</a>.</p> <h2>Summary</h2> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="689" height="457" src="https://blog.finxter.com/wp-content/uploads/2023/03/image-31.png" alt="" class="wp-image-1182653" srcset="https://blog.finxter.com/wp-content/uploads/2023/03/image-31.png 689w, https://blog.finxter.com/wp-content/uploads/2023/03/image-31-300x199.png 300w" sizes="(max-width: 689px) 100vw, 689px" /></figure> </div> <p>To edit a <code>file.txt</code> in the command line, use the command <code>notepad file.txt</code> to open a graphical editor on Windows.</p> <p>If you need a simple file edit in your terminal without a graphical editor and without installation, you can use the command <code>echo 'new content' > file.txt</code> that overwrites the old content in <code>file.txt</code> with <code>new content</code>. </p> <p>If you need a more direct in-CMD text editor run <code>copy con file.txt</code> to open the file in editing mode.</p> <p>If you’re SSH’d into a Unix machine, running the Vim console-based editor may be the best idea. Use <code>vim file.txt</code> or <code>vi file.txt</code> to open it.</p> <p>Feel free to join our <a href="https://blog.finxter.com/email-academy/" data-type="page" data-id="12278" target="_blank" rel="noreferrer noopener">email coding academy</a> (it’s free):</p> <p class="has-base-background-color has-background"> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/how-to-edit-a-text-file-in-windows-powershell/" data-type="post" data-id="236823" target="_blank" rel="noreferrer noopener">How to Edit a Text File in PowerShell (Windows)</a></p> </div> https://www.sickgaming.net/blog/2023/03/04/5-easy-ways-to-edit-a-text-file-from-command-line-windows/ |