Results 1 to 6 of 6

Thread: How to remove leading whitespace from each line?

  1. #1
    Join Date
    Dec 2009
    Posts
    40

    How to remove leading whitespace from each line?

    Hello friends,
    I have one problem in my file. I have one file which look like following:
    Code:
    for (a = 0; a < 100; a++)
        for (a = 0; a < 100; a++)
      for (a = 0; a < 100; a++)
           for (a = 0; a < 100; a++)
         for (a = 0; a < 100; a++)
               for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    I want to convert above file into following:
    Code:
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    How can this be done? Can anyone tell me how to remove leading whitespace from each line? Please help me.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to remove leading whitespace from each line?

    Hey it very simple to remove leading whitespace from each line. You have to just use one simple command in your unix editor to do this. You have to use sed command to do this. Just type following command into your unix and press enter.
    Code:
    sed "s/^[ \t]*//" -i filename

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: How to remove leading whitespace from each line?

    As per my information you have to use sed command to do this. It is very easy to use and implement. I have written following command for you. Just try to understand this.
    Code:
    $ sed 's/^ *//gs' < inputs.txt > outputs.txt
    Above command take two file one input file and output file. From input file we take original data and in output file we store modified data.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to remove leading whitespace from each line?

    It is very easy process to remove leading whitespace from each line. You have to just use following command in your unix editor to fix this problem.
    Code:
    $ sed 's/^[ \t]+//g' < input.txt > output.txt
    In above code "s" means "substitute". The /'s are the delimiters for the patterns. The value between the first two /'s are used to match pattern and data between the 2nd and 3rd / is the data to replace it with.

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to remove leading whitespace from each line?

    Hey there are two methods to do this. The first one is awk command. Using this you will get following output.
    you can use awk
    Code:
    $ awk '{$1=$1}1' file
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    The second one is sed command. You will get following output using this command.
    Code:
    $ sed 's|^[[:blank:]]*||gs' file
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)
    for (a = 0; a < 100; a++)

  6. #6
    Join Date
    Dec 2009
    Posts
    23

    Re: How to remove leading whitespace from each line?

    Do you want to preserve "empty" lines or delete them ?

    Here is an alternate biterscripting script with line-by-line control.

    Code:
    
    # Script LeadingWhiteSpaces.txt
    var str file, content, line, output
    cat $file > $content ; lex "1" $content > $line
    # Go line by line.
    while ($line <> "")
    do
        # Extract the line starting at the first printable char.
        stex -r -c "[^.^" $line >> $output
        lex "1" $content > $line
    done
    echo $output > { echo $file }
    



    Run script as

    Code:
    script "LeadingWhiteSpaces.txt" file("C:/somefolder/somefile.cpp")

    Will clean the file C:/somefolder/somefile.cpp of beginning white spaces ... assuming you saved the script in some folder in your $path as "LeadingWhiteSpaces.txt".

Similar Threads

  1. How to remove black horizontal line in openoffice
    By Efrosini in forum Windows Software
    Replies: 3
    Last Post: 17-02-2012, 04:55 PM
  2. How can I Remove the Leading Zeros in SQL?
    By PsYcHo 1 in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 06:59 AM
  3. How to remove the blank line in excel?
    By Rup_me in forum Windows Software
    Replies: 5
    Last Post: 16-12-2009, 06:48 PM
  4. How to use regex to remove whitespace in C#
    By AbhayD in forum Software Development
    Replies: 3
    Last Post: 25-08-2009, 11:58 PM
  5. Remove new line from vbscript
    By LucasYew in forum Software Development
    Replies: 2
    Last Post: 02-05-2009, 09:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,906,674.11769 seconds with 17 queries