Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove prefix of each line in a file and output to another file using sed

Tags:

bash

unix

sed

I have a source code file in which comments are prefixed with // (ie. double slashes and an empty space), I want to convert the source code into a document so I tried to cat file.c and pipe it to sed, the thinking is to replace "double slash and a space" if a line starts with it, with empty string, but it looks like the slash has some special meaning in sed, so what's the best way of constructing the sed arguments?

Thanks!

like image 447
hzxu Avatar asked Oct 24 '25 14:10

hzxu


2 Answers

The slash only has meaning if you allow it.

sed 's#^// +##' < file.c
like image 113
Ignacio Vazquez-Abrams Avatar answered Oct 26 '25 03:10

Ignacio Vazquez-Abrams


If you want to remove the special meaning of / from sed then following may help you in same.

sed 's/^\/\/ //g'  Input_file

So I am escaping / here by using \ before it, so it will be taken as a literal character rather than it's special meaning in code. Also if you are happy with above command's result then use -i to save the changes in Input_file itself. Hope this helps.

like image 38
RavinderSingh13 Avatar answered Oct 26 '25 03:10

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!