Computing.Net > Forums > Unix > Get rid of the leading space

Get rid of the leading space

Reply to Message Icon

Original Message
Name: Ou Huang
Date: January 20, 2003 at 07:55:51 Pacific
Subject: Get rid of the leading space
OS: AIX
CPU/Ram: ??
Comment:

For example, if I have a file which has following lines:
Dec 31 15:57 bjs_factsprint_17.txt
Jan 06 18:23 bjs_factsprint_18.txt

Is there any way to get rid of the leading space on the second line? So output file will become:
Dec 31 15:57 bjs_factsprint_17.txt
Jan 06 18:23 bjs_factsprint_18.txt

Thanks for help!


Report Offensive Message For Removal

Response Number 1
Name: deeps
Date: January 20, 2003 at 08:26:41 Pacific
Subject: Get rid of the leading space
Reply: (edit)

To remove leading space in a file. Just give this command in vi editor after opning that file.

:%s/^ *//

and all leading space shall be removed.

For trailing blanks the same way

:%s/ *$//

Hope this helps

Deeps.



Report Offensive Follow Up For Removal

Response Number 2
Name: Ou Huang
Date: January 20, 2003 at 09:57:51 Pacific
Subject: Get rid of the leading space
Reply: (edit)

Thanks for your quick response but I'd like to run it in a script I wrote. Can you help?

Thanks!


Report Offensive Follow Up For Removal

Response Number 3
Name: Jimbo
Date: January 20, 2003 at 15:55:39 Pacific
Subject: Get rid of the leading space
Reply: (edit)

Use sed:

sed 's/^ *//' your_file

-jim


Report Offensive Follow Up For Removal

Response Number 4
Name: Frank
Date: January 21, 2003 at 00:01:52 Pacific
Subject: Get rid of the leading space
Reply: (edit)

Or if you don't want to use sed, e.g. for performance reason,
you can use:

{ while read line ; do
output=${line# *}
echo $output
done } < xay

No Risk no fun

Frank


Report Offensive Follow Up For Removal

Response Number 5
Name: James Boothe
Date: January 23, 2003 at 09:49:49 Pacific
Subject: Get rid of the leading space
Reply: (edit)

There are some things here that can stand some clarification and warnings ...

The vi and sed solutions above will remove ALL leading spaces on each line. If desired, they could be changed to delete just the first space in each line by dropping that asterisk, which in an RE means zero or more of the preceding character.

You can demo this in vi on a file that has some leading spaces in some of the lines. The following search:

/^ *

will find the beginning of EACH line because we are asking for begin-of-line followed by zero-or-more spaces. But if you do the same search without the asterisk (search pattern is carat-space), it will find only those lines that begin with a space (does not matter what if anything follows the space).

The while-read solution deletes only a single leading space from each line. The asterisk in this context is not the same as the vi and sed solutions. It actually stands for any and all characters, just like when you do:

ls xyz*

The following korn shell expressions are identical:

${line# *}
${line# }

The first evaluates to the value of $line after removing the SHORTEST qualifying pattern at the beginning of $line that begins with a space and followed by any and all characters (which, for lines beginning with one or more spaces, will end up being a single space).

The second expression likewise removes the shortest qualifying expression from $line, the expression being a single space.

The expression:

${line## *}

(with two pound signs) removes the LONGEST qualifying expression of space-asterisk which means a space followed by everything. In other words, that expression would totally nullify each line beginning with a space.

I see that the while-read is somewhat better performance (at least on HP-UX) than the sed, but the BIG problem with the while-read solution is that it does not preserve your spacing, such as if you had multiple spaces between fields. That can be easily corrected with double-quotes, and we don't need that assignment to line either:

echo "${line# *}"

But to get this solution to eliminate ALL the leading spaces is a bit trickier.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Get rid of the leading space

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software