Unix Shell - Space deletion

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2006 > Space deletion





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Space deletion
Shahnaz

2006-12-01, 7:25 am

Hi Friends,
Can you please let me know how to delete all the spaces in a line of
text

eg:
1
o/p has to be
1

Thanks,
Shah

Bo Yang

2006-12-01, 7:25 am

Shahnaz :
> Hi Friends,
> Can you please let me know how to delete all the spaces in a line of
> text
>
> eg:
> 1
> o/p has to be
> 1


cat file | tr -d [:blank:]


Stephane CHAZELAS

2006-12-01, 7:25 am

2006-12-01, 20:22(+08), Bo Yang:
> Shahnaz :
>
> cat file | tr -d [:blank:]

[...]

tr -d '[:blank:]' < file

Remember '[' is a globbing operator to the shell so must be
quoted and cat is the command to concatenate files.

--
Stéphane
Bo Yang

2006-12-01, 7:25 am

Stephane CHAZELAS :
> 2006-12-01, 20:22(+08), Bo Yang:
> [...]
>
> tr -d '[:blank:]' < file
>
> Remember '[' is a globbing operator to the shell so must be
> quoted and cat is the command to concatenate files.
>

Thanks
Chris F.A. Johnson

2006-12-01, 1:20 pm

On 2006-12-01, Shahnaz wrote:
> Hi Friends,
> Can you please let me know how to delete all the spaces in a line of
> text
>
> eg:
> 1
> o/p has to be
> 1


Where do you have the line of text? If it is in a file:

tr -d ' ' < FILE


If it is in a variable:

var=" 1 2 3 "
oldIFS=$IFS
set -f
IFS=' '
set -- $var
IFS=
var=$*
IFS=$oldIFS


If it's in a file, and there is only one line in the file, read it
into a variable and use the method for a variable:

read var < FILE

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Rich Gibbs

2006-12-01, 1:20 pm

Bo Yang said the following, on 12/01/2006 07:22 AM:
> Shahnaz :
>
> cat file | tr -d [:blank:]
>
>


The '[:blank:]' set deletes all horizontal white space, not just
blank/space characters, which may or may not be what was wanted. Also,
why not just:

tr -d [:blank:] < file

--
Rich Gibbs
richg74@gmail.com
"If you find yourself in a hole, stop digging." (Will Rogers)


Rich Gibbs

2006-12-01, 1:20 pm

Stephane CHAZELAS said the following, on 12/01/2006 08:00 AM:
> 2006-12-01, 20:22(+08), Bo Yang:
> [...]
>
> tr -d '[:blank:]' < file
>
> Remember '[' is a globbing operator to the shell so must be
> quoted and cat is the command to concatenate files.
>


The '[' is, effectively, an alias for 'test'(1) when surrounded by
spaces. The sequence '[:blank:]' without the quotes works just fine:

rgibbs@rich01:~$cat try1.txt
Here is a line with some spaces.
And this one has two tabs.
rgibbs@rich01:~$tr -d [:blank:] <try1.txt > out1.txt
rgibbs@rich01:~$cat out1.txt
Hereisalinewithsomespaces.
Andthisonehastwotabs.
rgibbs@rich01:~$

This is with bash on Ubuntu Linux 6.06.

--
Rich Gibbs
richg74@gmail.com



Stephane CHAZELAS

2006-12-01, 1:20 pm

2006-12-01, 12:20(-05), Rich Gibbs:
> Bo Yang said the following, on 12/01/2006 07:22 AM:
>
> The '[:blank:]' set deletes all horizontal white space


That is <Space> and <Tab> (in most locales).

> not just
> blank/space characters, which may or may not be what was wanted. Also,
> why not just:
>
> tr -d [:blank:] < file


tr -d '[:blank:]' < file


--
Stéphane
Stephane CHAZELAS

2006-12-01, 1:20 pm

2006-12-01, 12:45(-05), Rich Gibbs:
> Stephane CHAZELAS said the following, on 12/01/2006 08:00 AM:
>
> The '[' is, effectively, an alias for 'test'(1) when surrounded by
> spaces. The sequence '[:blank:]' without the quotes works just fine:


[:blank:] is expanded by the shell into the list of filenames in the
current directory that are either of ":", "b", "l", "a", "n" or
"k".

When there is no such file in the current directory, some shells
will leave [:blank:] as it is (hiding the potential problem),
and some will report an error.

--
Stéphane
romy

2006-12-02, 1:16 pm

try this :

sed 's/ //g' filename > output_file

Shahnaz

2006-12-04, 7:20 am

Thank you so much.

Stephane CHAZELAS wrote:
> 2006-12-01, 20:22(+08), Bo Yang:
> [...]
>
> tr -d '[:blank:]' < file
>
> Remember '[' is a globbing operator to the shell so must be
> quoted and cat is the command to concatenate files.
>=20
> --=20
> St=E9phane


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com