質問

I am trying to delete n number of spaces between the two words, I was wondering if it’s possible. I searched over the net but couldn't found anything for this. I only find how to delete tab and blank lines. After I post the question 3 spaces get changet to one, please consider it as three in input.txt.

there are 3 space characters present in between "aaaa" and "bbb" ,I only want to delete two spaces.

$ cat Input.txt
aaaa   bbb   ccc ddd
eeee ff   ggg hhh

Desired output:

aaaa bbb ccc ddd
eee ff ggg hhh
役に立ちましたか?

解決

tr has the -s option to squeeze repeated characters:

$ tr -s ' ' < file
aaaa bbb ccc ddd
eeee ff ggg hhh

-s, --squeeze-repeats

replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character

Another example:

$ echo "aaa ----- bbbb ------------- ccc" | tr -s '-'
aaa - bbbb - ccc

他のヒント

try these lines, it may help:

tr -s ' ' 

or

sed 's/ \+/ /g' file

or

awk '$1=$1' file

Fedorqui's solution is the best approach but here is how with sed:

% sed 's/  */ /g' file
aaaa bbb ccc ddd
eeee ff ggg hhh
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top