Removing whitespace from files

Hello,
I was wondering if there was a way to write a script to do the following:

turn a file that contains:

1234 Kevin Smith 12:09 456235
1234 John Robger 12:09:09 353657

into:

1234%Kevin%Smith%12:09%456235
1234%John%Robger%12:09:09%353657

So far I can use sed to replace all the whitespace characters with % but I end up with
1234%%%%%%%%Kevin%%%%%%%%%%Smith%%%%%%12:09%%%%%456235

But I only want one % to seperate the fields.

Thanks.

try

==========================

#!/usr/bin/perl

open FILE, "<file.dat";
open FILE2, ">file2.dat";
while (<FILE>) {
print FILE2 join('%', split(' ', $_)), "\n";
}
close FILE;
close FILE2;

==========================

output data in file2.dat

Try this:

while read LINE; do
 echo $LINE | sed 's/ /%/g'
done < file > file2

When you echo a variable that contains multiple spaces, but you don't enclose the variable in double quotes, the multiple spaces are reduced down to one space. Then you can replace each single space with a percent sign. The resulting lines are placed in file2.

To turn a series of spaces into a percent sign, try this....

sed 's/&nbsp&nbsp */%/g' < input

NB, there are two spaces before the asterisk.

I kept trying it with one space and an asterisk, like 's/ */%/g' and it stuck a percent sign before every character.. why does two spaces do the trick?

"space asterisk" means zero or more spaces. "space space asterisk" means a space followed by zero or more spaces.

My personal fav for removing blank lines...not your question... but I thought it still might be relevant.

grep -v ^$ file.in > file.out

^ designates the beginning of the line.
$ designates the end of the line.

Now to your question.

awk '{ print $1 "\%" $2 "\%" $3 "\%" $4 ]' file.in > file.out

vi yourfile
:%s/ /\%/g

does the job perfectly!

But this only removes empty lines, not lines filled with spaces and tabs. Better:

awk 'NF > 0' file.in > file.out

thanks for restating my post.... IF you will re-read the first line of my post, you will notice I did say that this portion only removes blank lines..

My second example is for removing blanks in a data file. Assuming that there is no blank lines at the end of the line. You can expand this to work with data files that have upto 9 fields...

Just trying to show that there are different examples that work depending on your file... even though some are more encompassing than others...

And another version by perl:

perl -pe "s/ /%/g" < initial.file > output.file