Remove leading, trailing, and multiple spaces within a line

I would like to remove all leading and trailing spaces. In addition to replacing multiple spaces with one space within a line, all words in the line are separated by exactly one space.

I could achieve this using the following iteration of the regular expression and finding a solution to one regular expression .

s/^\s+|\s+$//g s/\s+/ /g 

Input Example:

  word1 word2 word3 word4 

Output Required:

 word1 word2 word3 word4 

It would be noticeable if you could help me solve this problem.

+6
source share
5 answers

You can use something like:

 s/^\s+|\s+$|\s+(?=\s)//g 

\s+(?=\s) will match all spaces in the middle of the line and leave one.

+8
source

Using awk

 echo " word1 word2 word3 word4 " | awk '{$1=$1}1' word1 word2 word3 word4 

This $1=$1 is a trick to concentrate everything.

You can even use

 awk '$1=$1' file 

But if the first field is 0 or 0.0 , it will fail

+3
source

This may work for you (GNU sed):

 sed -r 's/((^)\s*(\S))|((\S)\s*($))|(\s)\s*/\2\3\5\6\7/g' file 

or simply:

 sed -r 's/(^\s*(\S))|((\S)\s*$)|(\s)\s*/\2\4\5/g file 
+2
source

In Javascript, a string prototype has two methods that can control this:

 str.trim().replace(/\s+/g, ' ') 

str.trim() will remove leading and trailing spaces

str.replace(regex, replacement) will return a new line (non-destructive for the original str ), where regex will be compared with the provided string, and the first instance of the match will be replaced with replacement , then the whole new line will be returned.

It is important to note: the first .replace parameter .replace not be enclosed in quotation marks. The regular expression is divided by slashes ( /regex/ ), and then g added to mean replacing globally (each matched instance), and not just replacing the first or next instance based on lastIndex (which is initially 0, which gives the first instance). You can learn more about lastIndex and everything that I mentioned in the second link.

Example:

 var str = ' 1 2 3 4 ' function trimReplace(str){ newStr = str.trim().replace(/\s+/g, ' '); console.log(newStr); } trimReplace(str) 

Try this in the console: ' 1 2 3 4 '.trim().replace(/\s+/g, ' ')

"1 2 3 4"

_

regex: kleene statements help you understand the regex used to match multiple spaces

regex: a useful guide to regular expression and / g flag

Google: MDN string.protoype.trim ()

Google: MDN string.prototype.replace ()

+2
source

If you are on UNIX, you can use the Word-splitting shell. Bash lookup usage example below

 STR=" word1 word2 word3 word4 " z=$(echo $STR) echo "$z" word1 word2 word3 word4 
0
source

Source: https://habr.com/ru/post/954652/


All Articles