Sangamesh_R
Sangamesh_R

Reputation: 1

Delete all lines containing double forward slashes

I have a sample text file like which contains lines like below:

/home0/abc/xyz/Example1.java:               //  log.error("error" , exception);
/home0/abc/xyz/Example2.java:                   log.error("error" , exception);
/home0/abc/xyz/Example3.java:               //  log.error("error" , exception);
/home0/abc/xyz/Example4.java:                   log.error("error" , exception);

How can i delete all the lines containing double forward slashes (that is lines containing comments )in shell script.

My final output should like below:

/home0/abc/xyz/Example2.java:                   log.error("error" , exception);
/home0/abc/xyz/Example4.java:                   log.error("error" , exception);

Upvotes: 0

Views: 2457

Answers (3)

Esakki Thangam
Esakki Thangam

Reputation: 355

In grep you should use -v option like this

grep -v pattern filename

-v -used for invert the pattern matching.

In sed

  sed '/\/\//d' filename

d -for delete the pattern matched line.

Upvotes: 3

vijayant
vijayant

Reputation: 31

In a vi or vim editor, do this:

:g/\/\//d

which is: colon, g, slash, back-slash, slash, back-slash, slash, slash, d

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174706

You could use grep,

grep -v '//' file

From grep --help,

-v, --invert-match        select non-matching lines

Through sed,

sed '/\/\//d' file

The above sed command would delete those lines which contain two forward slashes. Syntax of the above sed is, sed '/regex/d' file. Because / is used as sed delimiters, escape the slashes present in your regex in-order to match a literal forward slash.

Through awk,

awk '!/\/\//' file

! negates the pattern and forces the awk to print the lines which won't contain //

Upvotes: 3

Related Questions