Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove space from string? [duplicate]

Tags:

bash

shell

In ubuntu bash script how to remove space from one variable

string will be

   3918912k  

Want to remove all blank space.

like image 484
Hitul Mistry Avatar asked Dec 01 '12 12:12

Hitul Mistry


People also ask

How do I remove double spacing from a string?

Using regexes with "\s" and doing simple string. split()'s will also remove other whitespace - like newlines, carriage returns, tabs.

How do I remove spaces from a string?

strip()—Remove Leading and Trailing Spaces. The str. strip() method removes the leading and trailing whitespace from a string.

How do I remove spaces between words in a string?

In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.


Video Answer


1 Answers

The tools sed or tr will do this for you by swapping the whitespace for nothing

sed 's/ //g'

tr -d ' '

Example:

$ echo "   3918912k " | sed 's/ //g' 3918912k 
like image 131
squiguy Avatar answered Oct 01 '22 11:10

squiguy