Removing whitespace from files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing whitespace from files
# 1  
Old 06-07-2003
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.
# 2  
Old 06-07-2003
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
# 3  
Old 06-07-2003
Try this:
Code:
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.
# 4  
Old 06-07-2003
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.
# 5  
Old 06-07-2003
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?
# 6  
Old 06-08-2003
"space asterisk" means zero or more spaces. "space space asterisk" means a space followed by zero or more spaces.
# 7  
Old 06-09-2003
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

Hi, I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script. script was return as follows. find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f find /cer_skyliv??/log... (6 Replies)
Discussion started by: venkat918
6 Replies

2. UNIX for Dummies Questions & Answers

Help in Removing the Old files

Hi Gurus, we are planning to clear the old log files based on the year and i need help on this and i searched in google and i came up with the scripts but i am stuck with this. (1) wroks fine How many files exist in based on the extension find -type f | sed -e 's/.*\.//' | sort | uniq... (1 Reply)
Discussion started by: SeenuGuddu
1 Replies

3. UNIX for Dummies Questions & Answers

Removing files

How do you delete/remove multiple files ? (5 Replies)
Discussion started by: nosuchluck
5 Replies

4. Shell Programming and Scripting

Removing whitespace issue

Hi, I have a file with rows like below delimited with pipe (|) I want to remove all the leading and trailing white space from each and every fields keeping the delimiter intact. I have tired this sed 's/*//g;s/*$//g' but the result is incorrect it is removing a whitespace from... (6 Replies)
Discussion started by: COD4
6 Replies

5. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

6. Shell Programming and Scripting

Help with removing files

i have a directory that have files that contains word "spam", how can i remove all those files which have word spam. This code help me in searching find ./ -type f -exec grep -l "spam" {} \; How i will add removing option with it. If some one have good suggestion regarding searching... (2 Replies)
Discussion started by: learnbash
2 Replies

7. Shell Programming and Scripting

Copy files listed in a text file - whitespace problem.

Hi, Say I have this text file <copy.out> that contains a list of files/directories to be copied out to a different location. $ more copy.out dir1/file1 dir1/file2 dir1/file3 "dir1/white space" dir1/file4 If I do the following: $copy=`more copy.out` $echo $copy dir1/file1... (4 Replies)
Discussion started by: 60doses
4 Replies

8. Shell Programming and Scripting

removing old files except configuration files and folders

Dear all, I want to remove files older than 2 months in the /home/member directory. But except the configuration files (like .bash_profile .config/ .openoffice/ .local/ .kde/ etc..) I have tried with the command find . -mtime +60 -wholename './.*' -prune -o -print -exec mv {} \; but it... (1 Reply)
Discussion started by: jamcalicut
1 Replies

9. Shell Programming and Scripting

removing whitespace from middle of file -help

I have a file in which I clean out a bunch of nonsense text as well as path information. What I end up with is something like the following: johnson.........................................................933 Where the periods represent the whitespace The file comes out originally with... (2 Replies)
Discussion started by: roninuta
2 Replies

10. UNIX for Dummies Questions & Answers

removing files

Hello all, I'd like to remove files which is returned by the following statement ls -l arch*.dbf|grep "`date|cut -c5-10`" (cut -c5-10 =Mar 20) To achive this,I tried the following statments but none worked .. rm < `ls -l arch*.dbf|grep "`date|cut -c5-10`"` rm `ls -l arch*.dbf|grep... (8 Replies)
Discussion started by: luft
8 Replies
Login or Register to Ask a Question