Skip to main content
wrong logic for the awk one. Note about `-r` being GNU
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Using sed:

sed '/[0-9]/!s/ //g' filename

This would remove spaces on all lines that do not contain a digit.

Using awk:

awk '!/[^0[0-9]/{gsub(" ", "", $0)}1';1' filename

For removing the space only between the first two words (here using GNU sed for -r, use -E instead on BSDs):

sed -r '/[0-9]/!s/([^ ]+) ([^ ]+)/\1\2/' filename

Using sed:

sed '/[0-9]/!s/ //g' filename

This would remove spaces on all lines that do not contain a digit.

Using awk:

awk '/[^0-9]/{gsub(" ", "", $0)}1' filename

For removing the space only between the first two words:

sed -r '/[0-9]/!s/([^ ]+) ([^ ]+)/\1\2/' filename

Using sed:

sed '/[0-9]/!s/ //g' filename

This would remove spaces on all lines that do not contain a digit.

Using awk:

awk '!/[0-9]/{gsub(" ", "", $0)};1' filename

For removing the space only between the first two words (here using GNU sed for -r, use -E instead on BSDs):

sed -r '/[0-9]/!s/([^ ]+) ([^ ]+)/\1\2/' filename
added 69 characters in body
Source Link
devnull
  • 10.8k
  • 2
  • 43
  • 50

Using sed:

sed '/[0-9]/!s/ //g' filename

This would remove spaces on all lines that do not contain a digit.

Using awk:

awk '/[^0-9]/{gsub(" ", "", $0)}1' filename

For removing the space only between the first two words:

sed -r '/[0-9]/!s/([^ ]+) ([^ ]+)/\1\2/' filename

Using sed:

sed '/[0-9]/!s/ //g' filename

This would remove spaces on all lines that do not contain a digit.

For removing the space only between the first two words:

sed -r '/[0-9]/!s/([^ ]+) ([^ ]+)/\1\2/' filename

Using sed:

sed '/[0-9]/!s/ //g' filename

This would remove spaces on all lines that do not contain a digit.

Using awk:

awk '/[^0-9]/{gsub(" ", "", $0)}1' filename

For removing the space only between the first two words:

sed -r '/[0-9]/!s/([^ ]+) ([^ ]+)/\1\2/' filename
Source Link
devnull
  • 10.8k
  • 2
  • 43
  • 50

Using sed:

sed '/[0-9]/!s/ //g' filename

This would remove spaces on all lines that do not contain a digit.

For removing the space only between the first two words:

sed -r '/[0-9]/!s/([^ ]+) ([^ ]+)/\1\2/' filename