Delete Multiple White Spaces


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Delete Multiple White Spaces
# 1  
Old 03-01-2007
Power Delete Multiple White Spaces

Hi,

I have a file that has multiple spaces between characters. I want to delete or convert the multiple spaces into a single space. I think this can be done in "sed" but I only know the syntax to delete trailing or leading spaces. Can this be done with "sed" or awk?

I have a file that looks like this:
Code:
02-15-07  02:59PM                11264 approvalcategory.rpt
02-15-07  02:59PM                12800 approvalclassaudit.rpt

I want it to look like this:
Code:
02-15-07 02:59PM 11264 approvalcategory.rpt
02-15-07 02:59PM 12800 approvalclass audit.rpt


Last edited by Yogesh Sawant; 04-21-2008 at 06:12 AM.. Reason: added code tags
# 2  
Old 03-01-2007
Quote:
Originally Posted by eja
Hi,

I have a file that has multiple spaces between characters. I want to delete or convert the multiple spaces into a single space. I think this can be done in "sed" but I only know the syntax to delete trailing or leading spaces. Can this be done with "sed" or awk?

I have a file that looks like this:

02-15-07 02:59PM 11264 approvalcategory.rpt
02-15-07 02:59PM 12800 approvalclassaudit.rpt

I want it to look like this:

02-15-07 02:59PM 11264 approvalcategory.rpt
02-15-07 02:59PM 12800 approvalclass audit.rpt
try this
Note: this works only if the number of spaces are same

Code:
sed -n "1,$s/               / /g" filename

# 3  
Old 03-01-2007
Code:
sed -e "s/[ \t]*/ /g" in.txt

\t stands for the tab character i.e. Ctrl-V+Tab
# 4  
Old 03-01-2007
Code:
sed "s/^ *//;s/ *$//;s/ \{1,\}/ /g" file

s/^ *// deletes spaces at the start of the line
s/ *$// deletes spaces at the end of the line
# 5  
Old 03-01-2007
Wow,

Thank you for such a quick response. I tried these and the 3rd suggested post works best for what I need.

Thank you again.
# 6  
Old 04-17-2008
A problem with space within " " which i don't want to remove

i am actually same kind of command to remove extra whitespaces, but in my file (.csv), i have couple of data with white space in between quotes.
(""). I don't want to remove those white spaces.

This is file:
Code:
"aaaaaa"         ,"aaaaaa"
"sss" ,"ddd"
"ddd" ,"F"   ,"fff      fffffF"

I want the output to be like this:
Code:
"aaaaaa","aaaaaa"
"sss","ddd"
"ddd","F","fff      fffffF"

But i am using :
Code:
$ sed "s/^ *//;s/ *$//;s/ \{1,\}//g" <data.csv> data1.csv

i am getting :
Code:
"aaaaaa","aaaaaa"
"sss","ddd"
"ddd","F","ffffffffF"

can anybody suggest anything so that i can keep the whitespaces inside "" (double quotes) but still remove the other spaces?

Thx.

Last edited by Yogesh Sawant; 04-21-2008 at 06:15 AM.. Reason: added code tags
# 7  
Old 04-18-2008
Use this script

Code:
sed -e's/" *,/",/g' -e's/, *"/,"/g'


Last edited by Yogesh Sawant; 04-21-2008 at 06:14 AM.. Reason: added code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List and Delete Files which are older than 7 days, but have white spaces in file name

I need to list and delete all files in current older which are olderthan 7 days. But my file names have white spaces. Before deleting I want to list all the files, so that I can verify.find . -type f -mtime +7 | xargs ls -l {} But the ls command is the working on the files which have white... (16 Replies)
Discussion started by: karumudi7
16 Replies

2. Shell Programming and Scripting

Replace white spaces of multiple files at once

How to rename multiple files by replacing the white spaces with underscore for ex: 293TrexFH\ \ \ GSM855007RINGB_lhb11_binary.txt 293TrexFH\ \ \ GSM855007RINGB_lhb12_binary.txt 293TrexFH\ \ \ GSM855007RINGB_lhb13_binary.txt 293TrexFH_GSM855007RINGB_lhb11_binary.txt... (1 Reply)
Discussion started by: quincyjones
1 Replies

3. Shell Programming and Scripting

Leading white spaces

Hi, I am having problem in deleting the leading spaces:- cat x.csv baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played $ nawk 'BEGIN{FS=","}!a... (2 Replies)
Discussion started by: scripter12
2 Replies

4. UNIX for Dummies Questions & Answers

handling white spaces with getopt

Hi I'm trying to ensure that I have catered for all situations with my getopt cases. One other situation I want to cover is should the user enter the script without any preceding arguments eg: ./script_eg I need the script to the direct the user to the helpfile I have tried... (3 Replies)
Discussion started by: ladyAnne
3 Replies

5. UNIX for Dummies Questions & Answers

Problem with White spaces and tabs

Hi All, I am facing issues converting white spaces and tabs together in a file I am reading. Here is the command I am trying: tr -s ' '@ | sort -t@ +1n filename I guess the problem is that it is not converting the tabs to another delimiter. Also, I am supposed to accomplish this only using... (5 Replies)
Discussion started by: sh_kk
5 Replies

6. Shell Programming and Scripting

Two or more white spaces in string

Hi, Can anybody suggest me how to combine two strings with two or more white spaces and assign it to a variable? E.g. first=HAI second=HELLO third="$first $second" # appending strings with more than one white spaces echo $third this would print HAI HELLO Output appears... (2 Replies)
Discussion started by: harish_oty
2 Replies

7. Shell Programming and Scripting

trimming white spaces

I have a variable that calls in a string from txt file. Problem is the string comes with an abundance of white spaces trailing it. Is there any easy way to trim the tailing white spaces off at the end? Thanks in advance. (9 Replies)
Discussion started by: briskbaby
9 Replies

8. Shell Programming and Scripting

Trim white spaces using awk

Hi, I have a CSV file with footer information as below. The third value is the number of records in the file. Sometimes it contains both leading and trailing white spaces which i want to trim using awk. C,FOOTER , 00000642 C,FOOTER , 00000707 C, FOOTER,... (2 Replies)
Discussion started by: mona
2 Replies

9. Shell Programming and Scripting

delete white spaces

hi all... i have the next question: i have a flat file with a lot of records (lines). Each record has 10 fields, which are separated by pipe (|). My problem is what sometimes, in the first record, there are white spaces (no values, nothing) in the beginning of the record, like this: ws ws... (2 Replies)
Discussion started by: DebianJ
2 Replies

10. UNIX for Dummies Questions & Answers

deleting white spaces

How would I delete white spaces in a specified file? Also, I'd like to know what command I would use to take something off a regular expression, and put it onto another. ie. . . . expression1 <take_off> . . . expression2 (put here) . . . Any help would be great, thanks! (10 Replies)
Discussion started by: cary530
10 Replies
Login or Register to Ask a Question