Removing double spaces in code


 
Thread Tools Search this Thread
# 1  
Removing double spaces in code

I want to be able to spot double spaces in code and then also replace it with a single space. Double spaces sometimes occur accidentally in code, but most often they are indents or seperate code from comments ("//"). In summary: I want to replace double spaces with single spaces where they do not appear at the beginning of the line and do not appear prior to "//".
How do I do this in for instance sed?
# 2  
try this
Code:
# sed '/  \/\/\|^  /!{s/  //g;!p}' infile

regards
ygemici
# 3  
Thank you for your prompt answer. First of all, I think it should be this:
Code:
sed '/  \/\/\|^  /!{s/  / /g;!p}' infile

ie replacement of a double space with a single space instead of removal of the double space ("s/ / /g").
Secondly, the following test case has issues:
Code:
   this is a line with double  space

is unaffected: lines with a double space and indented with spaces is unaffected.
How would I correct it?
# 4  
Code:
perl -pe 's/(?<=\S) +/ /g' infile

# 5  
Thank you for your answer. Your code also replaces double spaces prior to the comment delimiter ("//"). So that the following:
Code:
   this is some   code    // code comment

turns into the following
Code:
   this is some code // code comment

In brief there are the following cases:
Code:
this is code line   1
    this is code line  2   // code comment
    this is code line  3   // there may be good reasons to keep the double space in the   comment
    this is code line   4  // triple spaces should turn into one space as well

and should turn into the following:
Code:
this is code line 1
    this is code line 2   // code comment
    this is code line 3   // there may be good reasons to keep the double space in the   comment
    this is code line 4  // triple spaces should turn into one space as well

Grateful to anyone looking into this.
# 6  
Code:
[user@host ~]# cat input
this is code line   1
this is code line   1   // code   comment
    this is code line  2   // code comment
    this is code line  3   // there may be good reasons to keep the double space in the   comment
    this is code line   4  // triple spaces should turn into one space as well
[user@host ~]#
[user@host ~]# perl -lne 'chomp; if (/^(\s*?)(\S[^\/]*?)$/ || /^(\s*?)(\S.*?)(\s*\/\/.*)$/) { $x=$1;$y=$2;$z=$3; $y=~s/ +/ /g; print "$x$y$z" }' input
this is code line 1
this is code line 1   // code   comment
    this is code line 2   // code comment
    this is code line 3   // there may be good reasons to keep the double space in the   comment
    this is code line 4  // triple spaces should turn into one space as well
[user@host ~]#

This User Gave Thanks to balajesuri For This Post:
# 7  
That works as expected, thank you very much.

---------- Post updated at 17:34 ---------- Previous update was at 10:13 ----------

This problem is still vexing me. I believe there should be a simpler solution. In essence we are dealing with the following pattern:
Code:
[ ].*

a series of spaces
Code:
{code}

some code, with or without double / triple / etc spaces
Code:
[ ].*

(optionally) a series of spaces
Code:
// {comment}

(optionally) some comment with or without double / triple / etc spaces
The operation should be to leave 1, 3 and 4 alone and only replace the double spaces with single spaces from sub-pattern 2, the code. It should be possible to do this in a single sed statement.

Something like:
Code:
sed -e 's#\([ ].*\)\([a-z]*\)\([ ].*\)\(// .*\)#\1(s/  +/ /g \2)\3\4#' inputfile

Can someone give some pointers on how to do this?
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Medium
Zero factorial is equal to zero.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Removing spaces in the second field alone

Consider my input string as "abc|b f g|bj gy" I am expecting the output as "abc|bfg|bj gy". Please let me know how to achieve this in unix? Thanks (8 Replies)
Discussion started by: pandeesh
8 Replies

2. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

3. Shell Programming and Scripting

Removing spaces within Filename

Hello, I have a Folder (myfile) which contain the following files: P$12789865KR +N+01+OM+16102009165416.nu P$M1-508962GD +N+01+ALP+14102009094417.nu Is there a sed command(s) that will loop through this folder and remove the spaces that exists in the filename? Any help would be... (7 Replies)
Discussion started by: Fishn
7 Replies

4. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

5. Shell Programming and Scripting

Removing spaces from string

I want a user to be able to paste in a string like "01 3F 20 1F" and have the script reformat it to "013F201F" to pass it on to the next step. I was trying to figure it out with awk but wasnt working well. Never mind, found answer. did not know about tr :) (7 Replies)
Discussion started by: ippy98
7 Replies

6. UNIX for Dummies Questions & Answers

Removing spaces...

Hey, I'm using the command from this thread https://www.unix.com/unix-dummies-questions-answers/590-converting-list-into-line.html to convert vertical lines to horzontal lines. But I need to remove the spaces that is created. Unfortunately I can't figure out where the space is in the code.. I... (2 Replies)
Discussion started by: lost
2 Replies

7. UNIX for Dummies Questions & Answers

Removing Double Spaces for cut command

Hi I have a shell script that looks for running processes in order to kill them. The script (ksh) gets the PID of these processes using the following: PROCS=`ps -fu ${USERID} | egrep "MONITOR" | grep -v grep | cut -d" " -f4` However, I spotted an issue where PID's of different lengths... (3 Replies)
Discussion started by: mikem22
3 Replies

8. Shell Programming and Scripting

removing spaces

hey.. i had a problem with the unix command when i want to remove the white spaces in a string..i guess i cud do it with a sed command but i get an error when i give space in the square brackets.. string="nh hjh llk" p=`echo $string | sed 's/ //g'` i donno how to give space charater and... (2 Replies)
Discussion started by: sahithi_khushi
2 Replies

9. Shell Programming and Scripting

Removing spaces at particular position

I have a file with delimiter ~ ABC~12~43~TR ~890~poi~YU ~56~65 What I want is to remove spaces from column 4,7 and other columns as it is So, the final file becomes ABC~12~43~TR~890~poi~YU~56~65 (7 Replies)
Discussion started by: superprogrammer
7 Replies

10. Shell Programming and Scripting

removing spaces in a file

I have output a file and need to remove blank spaces in the first lines only so the file is left end justified. I need to do this while keeping the rest of the file intact. Example of file that needs spaces removed: space space space space space space space... (3 Replies)
Discussion started by: tioray
3 Replies

Featured Tech Videos