10

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

5 Answers5

26

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.

Kusalananda
  • 333,661
1

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
  • 1
    @Wildcard in general you are right, if any of the assumptions that you mention are violated this answer will not be right, and i totally agree with you that the other answer is the best answer, but, that doesn't mean that is not a right answer for this condition. – Wissam Roujoulah Jan 22 '17 at 13:26
  • 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
  • 4
    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
1

You can use Vim in Ex mode:

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

  2. s substitute

  3. x save and close

Zombo
  • 1
  • 5
  • 44
  • 63
0

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"                        
2015-04-1810:21:591005430-9999-9999000000000000

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:
    words=line.strip().split()
    print(words[0],end="")
    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

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.

TJM
  • 554