{"id":6,"date":"2012-06-17T06:37:00","date_gmt":"2012-06-17T06:37:00","guid":{"rendered":"http:\/\/pheonixsolutions.com\/?p=6"},"modified":"2026-09-12T12:31:04","modified_gmt":"2026-09-12T07:01:04","slug":"remove-a-space-in-front-of-a-words-in-a-file","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/remove-a-space-in-front-of-a-words-in-a-file\/","title":{"rendered":"How to Remove Leading Spaces from a File in Linux Using sed"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Introduction<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Spaces at the beginning of words or lines in a file can sometimes cause formatting issues or make it difficult to process the file correctly. Linux provides several commands that can be used to remove unwanted spaces. In this guide, we will use <code>cut<\/code>, <code>sort<\/code>, and <code>sed<\/code> commands to remove spaces from the beginning of words and display the cleaned output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, make sure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Access to a Linux server or system.<\/li>\n\n\n\n<li>Basic knowledge of Linux commands.<\/li>\n\n\n\n<li>A file containing the text that needs to be processed.<\/li>\n\n\n\n<li>Permission to read the input file and write to the output file, if required.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n<div dir=\"ltr\" style=\"text-align: left;\">This is a simple script to show how to remove a space in front of a word from a file. You can learn about this simple command for removing the space in front of a word and execute it<\/p>\n<p>from your end. I was trying to do this using various commands and scripts. But I was unsuccessful in doing it. Later, I used a simple method to remove the space in front of a word in a file. I&#8217;m very happy to share the same with you.<br \/><strong><br \/><\/strong>Just use this command to remove the space in front.<\/p>\n<h4>cat filename |cut -d&#8221;:&#8221; -f2 |sort | sed -e &#8216;s\/^[ \\t]*\/\/&#8217;<\/h4>\n<div style=\"margin: 0px; text-indent: 0px;\">cat filename &#8211; This will show you all the contents in a file name<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\">\u00a0<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\"><strong># cat filename<\/strong><\/div>\n<div style=\"margin: 0px; text-indent: 0px;\">\u00c2\u00a0 arun<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\">\u00c2\u00a0hemanth<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\">\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 :karthick<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\">madhan<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\">\u00c2\u00a0 Dhanasekar<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\">\u00a0<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\"><strong>cut -d&#8221;:&#8221; -f2<\/strong> &#8211; this will remove: if there are any before a word. -f2 is field 2<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\"><strong>sort &#8211;<\/strong> sorting the output in order<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\"><strong>sed -e &#8216;s\/^[ \\t]*\/\/&#8217;<\/strong><\/div>\n<div style=\"margin: 0px; text-indent: 0px;\">\u00a0<\/div>\n<div style=\"margin: 0px; text-indent: 0px;\"><strong># cat c |cut -d&#8221;:&#8221; -f2 |sort | sed -e &#8216;s\/^[ \\t]*\/\/&#8217;<\/strong><br \/>arun<br \/>Dhanasekar<br \/>hemanth<br \/>karthick<br \/>madhan<\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>Now it will show you the output. If you want the output added inside a file, just use this\u00c2\u00a0<br \/>\u00c2 cat filename |cut -d&#8221;:&#8221; -f2 |sort | sed -e &#8216;s\/^[ \\t]*\/\/&#8217; &gt; filename.txt<\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using the <code>cut<\/code>, <code>sort<\/code>, and <code>sed<\/code> commands, you can easily remove unwanted spaces from the beginning of words or lines in a file. The commands can also be combined with output redirection (<code>&gt;<\/code>) to save the processed content into a new file for further use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. How do I remove leading spaces from a file?<\/strong><br>Use <code>sed 's\/^[ \\t]*\/\/' filename<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Can I save the output to another file?<\/strong><br>Yes, use <code>&gt; filename.txt<\/code> at the end of the command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. Will this remove spaces between words?<\/strong><br>No, it removes only spaces or tabs at the beginning of each line.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-create-and-remove-a-symbolic-link-in-linux\/\">How to Create and remove a Symbolic Link in Linux <\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-convert-file-system-type-from-ext3-to-ext4-for-linux-servers\/\">How to convert file system type from ext3 to ext4 for Linux servers<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business?. Our experts can help with\u00a0web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team <a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Spaces at the beginning of words or lines in a file can sometimes cause formatting issues or make it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1020,1040],"tags":[261],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-containers-kubernetes","category-linux","tag-linux","psol-cat-containers-kubernetes"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-6","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":4,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":11709,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6\/revisions\/11709"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}