I'm trying to convert single line 'C' style comments to 'C++' style.  The 'sed' below isn't bad, but of course it fails if any leading code (code before the comment) has any ' / ' in it at all:  sed -i 's,\(^[^\/]*\)\/\*\([^\*]*\)\*\/[ ]*$,\1\/\/\2,' filename   What I wish I could do is this:  ... [^\\/\\*] ...   i.e. negate ' /* ' which doesn't work of course, but after several hours of searching, I can't find a simple explanation of how to do that properly :(  It doesn't seem like it should be rocket science.  For example, these strings:   blah blah        /* comment */ blah blah / blah /* comment */ blah blah        /* comment */ blah blah blah / blah /* comment */ blah    ... should convert thusly:  blah blah        // comment  blah blah / blah // comment  blah blah        /* comment */ blah  (this CAN'T be converted) blah blah / blah /* comment */ blah  (this CAN'T be converted)   ... obviously no conversion can take place if there is code AFTER the 'C' comment.  I will do a close visual comparison between the file, before and after, so there's no need to handle ' /* ' inside a literal, nor do I want to convert anything multi-line.   Note I think of this as a 'negation' problem but maybe there is another way.  I  just need to capture everything before a ' /* ' and I don't care how.  FOLLOW UP ON ANSWER BELOW  Well damn! I see that I've completely misunderstood something fundamental:   .*/\*   ... reads: "anything except slash star followed by slash star", so actually I get my 'negation' for free :-)   So, going even further than Barmar:  sed -i 's,^\(.*\)/\*\(.*\)\*/\s*$,\1//\2,' filename   ... will even catch this:  blah / * blah        /* co / mme * nt */   and output this:  blah / * blah       // co / mme * nt    Enlightenment.
122379
I am trying to write a script that will delete all comments and everything in between inside C files in my current directory. I've been using sed, and this is what I have so far:   sed -i '/ * [^()] */d' *.c  This works when the comments are on the same line as an asterisk or backslash.   However it doesn't work when there is a commented line without a slash or asterisk.   I know sed goes line by line, I just don't know how to tell it to keep deleting until it sees a */.
5286
I have a script that reads a text stream and generates a file of sed commands that is later run with sed -f. The generated sed commands are like:  s/cid:image002\.gif@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1922/g s/cid:image003\.gif@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1923/g s/cid:image004\.jpg@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1924/g   Assume the script which generates the sed commands is something like:  while read cid fileid do     cidpat="$(echo $cid | sed -e s/\\./\\\\./g)"     echo 's/'"$cidpat"'/https:\/\/mysite.com\/files\/'"$fileid"'/g' &gt;&gt; sedscr done   How can I improve the script to ensure all regex metacharacters in the cid string are escaped and interpolated properly?
96079
I am using the following code to read specific lines in an ASCII file "text.dat"  sed -n '/Columnaxis/p' text.dat   The output is   Columnaxis: 100   In order to output only the second part previous line, I added the following to the previous command line:   sed -n '/Columnaxis/p' text.dat | awk '{ print $2 }'   The output of the last command is "100"  How can I update the last code to output negative numbers, regardless of the number's original sign? ( i.e if the input is "+100" I want the code to output "-100" also if the input is "-100", I want the code to output "-100"  PS. The content of text.dat is something like the following :  Columnaxis: 100 Columnaxis_1: 100 Columnaxis_2: -100 Columnaxis_3: 50 Columnaxis_4: -2.3 Columnaxis_5: 1.2 Columnaxis_6: -5 Columnaxis_7: -2 Columnaxis_8: -4 Columnaxis_9: -2   I want the code to output the following:  Columnaxis: -100   Let's say that the input has "Columnaxis: +100", then I want it to be  Columnaxis: -100
96362
Recently, I asked a  how to sort pairs of lines, and one of the answers suggested concatenating lines with sed, like this:  cat file.txt | sed -n 'N;s/\n//;p' | sort -t";" -k43,43n | perl -F';' -ane '$,=";";print @F[0..13],"\n";print @F[14..$#F]'   which worked brilliantly, but now my problem generalized to sorting n-tupels of lines, which I can't figure out how to do with sed.   Everything I found was either for 2 lines or ALL lines, but I need n lines (where n is 5 at the moment, but a general way would be great).  Bonus points for a nice way of rewriting the perl part to accomodate n lines, but the problem is really about the sed part.  I am also not hung on sed specifically, so if you have a nice solution using a different command line tool, please post it.  Update: Example input (n == 3)  a1;b1;c1;  n1;m1;l1;  d1;e1;f1;g1 n2;m2;l2; a2;b2;c2; d2;e2;f2;g2
97125
I have the following string: /tmp/test/folder1/test.txt  I wish to use sed to substitute / for \/ - for example:  \/tmp\/test\/folder1\/test.txt   So I issue:  echo "/tmp/test/folder1/test.txt" | sed "s/\//\\\\//g"   Although it returns:     sed: -e expression #1, char 9: unknown option to `s'   I am escaping the forward slash and backslash - so not sure where I have gone wrong here
52362
Suppose the following chunk of code:  lines = text.split`\n`   Not relevant, but it is valid ES6 code  My question is: How to write it inside one comment? When I use backticks around content it displays the content formatted as code. But what if there are backticks inside this content?
60541
So I've been using 'sed' on linux for a while, but have had a bit of difficulty trying to use it on OSX since 'POSIX sed' and 'GNU sed' have so many little differences.  Currently I'm struggling with how to insert a line of text after a certain line number. (in this case, line 4)  On linux I would do something like this:  sed --in-place "4 a\  mode '0755'" file.txt   So on OSX I tried this:  sed -i "" "4 a\ mode '0755'" file.txt   However this keeps giving me a 'extra characters after \ at the end of a command' error.  Any ideas what's wrong here?  Do I have a typo?  Or am I not understanding another difference between versions of sed?
34120
I am looking for a way to replace a string in a file with a string that contains a slash by using sed.  connect="192.168.100.61/foo" srcText="foo.bar=XPLACEHOLDERX" echo $srcText | sed "s/XPLACEHOLDERX/$connect"   The result is:  sed: -e Expression #1, Character 32: Unknown option for `s'
8304
I have    Input:           NISHA =\455     Output:   NISHA = 455   I want to remove \ from output. I tried to use command sed "s/[\]//g" P but it is not working and it flags an error:  character found after backslash is not meaningful
64797
Situation: It started out as me  for the literal string C-c in  but it ended up omitting the - sign and showing results which contained the strings C/C++ and C &amp; C++.  is an example of a post which contains that string of characters (yet fails to show up in the results).  Things I've tried:   post suggests that I can use the undocumented code: operator, but when I do that, it just wraps the whole term in double quotes like .  As @terdon in Unix &amp; Linux chat has , using this  does yield some relevant results, but it doesn't show me  that is supposed to be there. edit: It appears that the Symbolhound query no longer returns that result.  Finally,  says that you can search for literal characters, but as you can see:    Quoted phrases are exact matches except for case-sensitivity, for example, you can search  or .  The second example (symbols) leads to 0 results.  So by now I assume that the search engine has changed (but the old documentation still lingers on this site). Is there a method for me to search for strings that include special (or is it literal?) characters?
105512
I can't find any documentation about the sed -e switch, for simple replace, do I need it?  e.g.  sed 's/foo/bar/'   VS  sed -e 's/foo/bar/'
111971
What special formatting tricks can be done in the comments now?  Proven:   **text** or __text__ = Bolding = text *text* or _text_ = Italics = text `text` = Code Formatting = text [example](http://example.com "title") Inline links with title and text http://example.com plain old links   Disproven:   Cannot insert &lt;kbd&gt; (i.e. F12) characters Cannot use #[text] to increase size
55644
I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)  I realize that I could .match() substrings with a Regex, but that doesn't solve nesting /*, or having a // inside a /* */.  Ideally, I would prefer a non-naive implementation that properly handles awkward cases.
100128
Here's a sample 'test.txt'  abcdX1yad45das abcdX2fad45das abcdX3had45das abcdX4wad45das abcdX5mad45das   sample desired output:  X1yad X2fad X3had X4wad X5mad   I could get it working in vim with:  :% s/\v.*(X\d)(.*)45.*/\1\2/   and worked in perl as well with:  open(my $file, "&lt;", "test.txt"); while(&lt;$file&gt;) {     s/.*(X\d)(.*)45.*/$1$2/;      print $_; }   my eventual regular expression needs two groupings, it wasn't required for this example output  am not able to get it work with sed:  sed -r 's/.*(X\d)(.*)45.*/\1\2/' test.txt abcdX1yad45das abcdX2fad45das abcdX3had45das abcdX4wad45das abcdX5mad45das   just to check sed is working,  sed -r 's/(a)/#\1#/' test.txt #a#bcdX1yad45das #a#bcdX2fad45das #a#bcdX3had45das #a#bcdX4wad45das #a#bcdX5mad45das   what am I doing wrong in sed?
73286
I have a file that contains multiple lines of xml. I would like to replace certain parts of the file. Some parts of the file contains quotation marks (") which I would like to replace. I have been trying to escape the quotation mark with \, but I don't think this is working based on the result of my file.  Here is an example of one of my sed commands:  sed -e "s/\"text\"/'text'/ig" file.xml &gt; temp.tmp   Is this how you escape quotation marks in a sed command or am I doing something wrong?
17639
Take the following script:  #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file]   If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need to escape the backslashes themselves (between the octets, or in the \s or \1). What's the rule here? What about when I need to use {...} or [...]? Is there a list of what I do and don't need to escape?
1874
How do I get a platform-dependent newline in Java? I can’t use "\n" everywhere.