Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete whitespace in each begin of line of file, using bash

How i can delete whitespace in each line of file, using bash For instance, file1.txt. Before:

  gg g
 gg g
t  ttt

after:

gg g
gg g
t  ttt
like image 386
G-71 Avatar asked Feb 16 '12 10:02

G-71


People also ask

How do I remove a space at the beginning of a line in bash?

Use sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]].

How do I remove extra spaces in bash?

You can enable shopt -s extglob and then just use NEWHEAD=${NEWHAED/+( )/ } to remove internal spaces.

How do I remove white spaces from a file in Linux?

s/[[:space:]]//g; – as before, the s command removes all whitespace from the text in the current pattern space.

How do I remove leading and trailing space in Unix?

sed command: The 1st command removes the leading spaces, the second removes the trailing spaces and the last replaces a group of spaces with a single space. The source file itself can be updated by using the -i option of sed.


1 Answers

sed -i 's/ //g' your_file will do it, modifying the file inplace.

To delete only the whitespaces at the beginning of one single line, use sed -i 's/^ *//' your_file

In the first expression, we replace all spaces with nothing. In the second one, we replace at the beginning using the ^ keyword

like image 61
Scharron Avatar answered Sep 30 '22 04:09

Scharron