How can I remove only the first space from a line like the one below without removing the other spaces in the same line?

Example Input:

2015-04-18 10:21:59 10 05430 -9999 -9999 000000000000

Example Output:

2015-04-1810:21:59 10 05430 -9999 -9999 000000000000

Using awk:

> awk '{print $1$2,$3,$4,$5,$6,$7}' yourfile > outputfile

The output:

> cat outputfile

2015-04-1810:21:59 10 05430 -9999 -9999 000000000000

You can remove any space in the line and keep any one

Assuming: Input file must contain:

  1. no leading whitespace
  2. no consecutive spaces
  3. no tabs
  4. less than seven spaces total

If any of this assumption are violated, the result will not be correct

  • 7
    Downvoted because this is utterly dependent on the following assumptions: Input file must contain (1) no leading whitespace (2) no consecutive spaces (3) no tabs (4) less than seven spaces total. If any of these assumptions are violated, the command does something quite different from the requested action: "Remove the first space from each line (without other changes)." – Wildcard Jan 22 '17 at 13:19
  • 1
    The proper solution has been posted. This answer is really not correct. – Wildcard Jan 22 '17 at 13:21
  • 4
    Keep in mind that this site exists to serve future readers, not just the current asker. So this is a very misleading answer to, "How do I remove the first space from each line of my file?" At the very least you should include the assumptions after the answer with a note, warnings. – Wildcard Jan 22 '17 at 13:28
  • 2
    @Wildcard again you are right, I edited my answer. – Wissam Roujoulah Jan 22 '17 at 13:37
  • 3
    Instead of all this verbosity, you could have used sub() function, which removes only first match in line, so your lengthy awk expression can be reduced to awk '{sub(/ /,"")};1' input.txt – Sergiy Kolodyazhnyy Jan 22 '17 at 21:04

You may use sed for this:

sed 's/ //' infile >outfile

This applies a substitution to all lines of the file infile that will substitute the first space character with nothing (i.e. remove it). The output is stored in the file outfile. With sed 's/ //N', where N is an integer between 1 and 9, you can pick which space to remove.

If the line is in a shell variable, you could use

var="${var/ /}"

This uses the ${parameter/pattern/string} parameter expansion in bash to do the same thing as the sed command, but on the value in $var. The resulting string is, in this example, then stored back into $var.


You can use Vim in Ex mode:

ex -sc '%s/ //|x' file
  1. % select all lines

  2. s substitute

  3. x save and close


Small and simple Perl one-liner:

$ perl -ane 'printf "%s",@F[0];@F[0]="";print @F'  <<< "2015-04-18 10:21:59 10 05430 -9999 -9999 000000000000"                        

Or even simpler as suggested by mik in comments:

perl -pe 's/ //' <<< "2015-04-18 10:21:59 10 05430 -9999 -9999 000000000000"

Python can do it too. Here's a small script that does the job. It can be reduced to a one-liner, if necessary:

#!/usr/bin/env python
from __future__ import print_function
import sys
for line in sys.stdin:
    print(" ".join(words[1:]))

And with the same line in input file duplicated 3 times, here's a test for multi-line input:

$ ./remove_leading_space.py < input.txt                                                                                               
2015-04-1810:21:59 10 05430 -9999 -9999 000000000000
2015-04-1810:21:59 10 05430 -9999 -9999 000000000000
2015-04-1810:21:59 10 05430 -9999 -9999 000000000000

If you just one single line edited, you can do it like so:

$ ./remove_leading_space.py <<< "2015-04-18 10:21:59 10 05430 -9999 -9999 000000000000"                                               
  • 1
    A small and simple perl oneliner would be perl -pe 's/ //' – mik Jan 24 '17 at 21:27
  • @mik Thank you. Edited into the answer. Still learning perl, so doing best I can with what i got :) – Sergiy Kolodyazhnyy Jan 24 '17 at 21:30

It's a quite simple question.

sed -i 's/ //' /path/to/file

Try this command and don't add s in the end of sed expression.

It will just remove the first space each line.

  • 2
    How's this different than Kusalananda's ? – don_crissti Jan 22 '17 at 21:32
  • @don_crissti while by using -i you can type less. And -i is the official recommended way if you just wanna change text files directly. – TJM Jan 22 '17 at 21:41
  • 3
    Yeah, next guy will add a -e and say it's different too... – don_crissti Jan 22 '17 at 22:25
  • @don_crissti Why not? I think it will provide more helpful information for those people who are in trouble like the questioner. – TJM Jan 23 '17 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.