Replacing double spaces with single space


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing double spaces with single space
# 1  
Old 09-17-2013
Replacing double spaces with single space

I am looking for a regular expression that uses sed to replace multiple spaces with single spaces on every line where it is not at the start of the line and not immediately before double slashes ('//') or between quotes (").
In its simplest form, it would look like this:
Code:
sed -e 's#  # #g' inputfile

It should therefore perform an inline replacement of this:
Code:
    series of  statements "  with double spaces" in between   // some comments here

into this (only one occurrence of double spaces replaced):
Code:
    series of statements "  with double spaces" in between   // some comments here

However, it should ignore any spaces at the start of the line:
Code:
^(\s)*

And it should ignore everything between quotes:
Code:
"((\\")|[^"(\\")])+"

And it should ignore all white spaces up to the double slashes:
Code:
\s*//

How would I combine all these regular expressions in a single command and using sed?
# 2  
Old 09-17-2013
First attempt
Code:
sed -r 's/([[:alnum:]])  ([[:alnum:]])/\1 \2/' file
    series of statements "  with double spaces" in between   // some comments here

(Doesn't work between double quotes.)
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-17-2013
Not a regular expression, nor with sed:
Code:
awk '
{
  for(i=1; i<=NF; i+=2) {
    n=split($i,F,"//")
    while(match(F[1],/[^ ]  +[^ ]/)) F[1]=substr(F[1],1,RSTART) " " substr(F[1],RSTART+RLENGTH-1)
    $i=F[1]
    for (j=2; j<=n; j++) $i=$i "//" F[j]
  }
}
1
' FS=\" OFS=\" file

--
sed with (repeated) regex, does not work 100% correctly within quotes, but it may be a step further:
Code:
sed -e :a -e 's/\([^ "]\)   *\([^ /"]\)\(\/\/.*\)*/\1 \2\3/;ta'  file


Last edited by Scrutinizer; 09-17-2013 at 08:05 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 09-18-2013
Code:
sed -r 's/([[:alnum:]])  ([[:alnum:]])/\1 \2/' file

I am willing to compromise on the quotes, since they are very rare in my input file. However, the solution does not work when there are more than two spaces. I tried the following, but that left the test line unchanged:
Code:
sed -r 's/([[:alnum:]])(\s*)([[:alnum:]])/\1 \3/'

Code:
sed -e :a -e 's/\([^ "]\)   *\([^ /"]\)\(\/\/.*\)*/\1 \2\3/;ta' file

That one works well, even for the occurrences of quotes that I have.
# 5  
Old 09-18-2013
Well, try this one
Code:
    series of      statements "  with double spaces" in between   // some comments here
sed -r 's/([[:alnum:]]) +([[:alnum:]])/\1 \2/g' file
    series of statements "  with double spaces" in between   // some comments here

This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the... (2 Replies)
Discussion started by: Hari Prasanth
2 Replies

2. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

3. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

4. Shell Programming and Scripting

Replacing trailing space with single quote

Platform : RHEL 5.8 I want to end each line of this file with a single quote. $ cat hello.txt blueskies minnie mickey gravity snoopyAt VI editor's command mode, I have used the following command to replace the last character with a single quote. ~ ~ ~ :%s/$/'/gNow, the lines in the... (10 Replies)
Discussion started by: John K
10 Replies

5. UNIX for Dummies Questions & Answers

How to translate multiple spaces into a single space using tr command?

I am trying to read a txt file and trying to translate multiples spaces into single spaces so the file is more organized, but whenever I try the command: tr ' ' ' ' w.txt The output is: tr: extra operand `w.txt' Try `tr --help' for more information. Can someone please help? :wall: ... (2 Replies)
Discussion started by: Nonito84
2 Replies

6. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies

7. Shell Programming and Scripting

Stripping out more than a space from a line, but keep single space.

Hi all, Is there a way to perform the above, I am trying to strip out more than one space from a line, but keep the single space. See below output example. My Name is test test2 test3 test4 test5 My Name is test test2 test3 test4 test5 Please note that the lines would contain... (7 Replies)
Discussion started by: eo29
7 Replies

8. Shell Programming and Scripting

replace space or spaces in a line of a file with a single :

I am searching while I await a response to this so if it has been asked already I apologize. I have a file with lines in it that look like: bob johnson email@email.org I need it to look like: bob:johnson:email@email.org I am trying to use sed like this: sed -e 's/ /:/g' file >... (5 Replies)
Discussion started by: NewSolarisAdmin
5 Replies

9. Shell Programming and Scripting

Consecutive spaces within input being converted to single space

I'm reading from a file that is semi-colon delimited. One of the fields contains 2 spaces separating the first and last name (4th field in - "JOHN<space><space> DOE"): e.g. TORONTO;ONTARIO;1 YONGE STREET;JOHN DOE;CANADA When I read this record and either echo/print to screen or write to... (4 Replies)
Discussion started by: NinersFan
4 Replies

10. Shell Programming and Scripting

replacing single space in argument

I want to write a script which will check the arguments and if there is a single space(if 2 more more space in a row , then do not touch), replace it with _ and then gather the argument so, program will be ran ./programname hi hello hi usa now hello hello so, inside of program,... (7 Replies)
Discussion started by: convenientstore
7 Replies
Login or Register to Ask a Question