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?
try this
# sed '/ \/\/\|^ /!{s/ //g;!p}' infile
regards
ygemici
Thank you for your prompt answer. First of all, I think it should be this:
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:
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?
perl -pe 's/(?<=\S) +/ /g' infile
Thank you for your answer. Your code also replaces double spaces prior to the comment delimiter ("//"). So that the following:
this is some code // code comment
turns into the following
this is some code // code comment
In brief there are the following cases:
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:
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.
[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 ~]#
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:
[ ].*
a series of spaces
{code}
some code, with or without double / triple / etc spaces
[ ].*
(optionally) a series of spaces
// {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:
sed -e 's#\([ ].*\)\([a-z]*\)\([ ].*\)\(// .*\)#\1(s/ +/ /g \2)\3\4#' inputfile
Can someone give some pointers on how to do this?