LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to delete the lines containing "./." (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-the-lines-containing-925490/)

linuxon 01-24-2012 12:44 PM

how to delete the lines containing "./."
 
Hello everyone,

I am new to linux, I know how to delete the one special character in linux, but i don't know how to delete the lines containing character like "./.". hopefully someone could give some instruction.

Thanks a lot

kabamaru 01-24-2012 12:54 PM

This will preview the changes.
Code:

sed '/\.\/\./d' name_of_your_file
This will create a new file with the changes. Original file will remain as is.
Code:

sed '/\.\/\./d' name_of_your_file > new_file
This will edit the original file
Code:

sed -i '/\.\/\./d' name_of_your_file

sycamorex 01-24-2012 01:24 PM

Quote:

Originally Posted by kabamaru (Post 4583342)
This will preview the changes.
Code:

sed '/.\/./d' name_of_your_file
This will create a new file with the changes. Original file will remain as is.
Code:

sed '/.\/./d' name_of_your_file > new_file
This will edit the original file
Code:

sed -i '/.\/./d' name_of_your_file

The above code will delete any line containing /
Not only ./.

You'd have to escape . as well:

Code:

sed '/\.\/\./d' file

linuxon 01-24-2012 01:24 PM

Thanks a lot, kabamaru, looks like the last one works

sycamorex 01-24-2012 01:26 PM

Quote:

Originally Posted by linuxon (Post 4583361)
Thanks kabamaru, that deleted everything in the file when I tried it. Is there any other way?

See my code above.

kabamaru 01-24-2012 01:54 PM

Quote:

Originally Posted by sycamorex (Post 4583360)
The above code will delete any line containing /
Not only ./.

You'd have to escape . as well:

Code:

sed '/\.\/\./d' file

You're right. The funny thing is I wrote it correctly in my terminal, then wrote it 3 times wrong in the post :-/

David the H. 01-25-2012 06:26 AM

To get around conflicts with "/" in sed, you can use a different delimiter.

In the substitution command, you can simply use any ascii character (except for newline and null) instead of the slash. I generally like to use "|" myself.

Code:

sed 's|.*\./\..*||' file        #for demonstration purposes, it leaves a blank line behind
You can also change the delimiter for the address fields, except that you must precede the first delimiter with a backslash.

Code:

sed '\|\./\.|d' file
Finally, instead of backslash-escaping regex-reserved characters, consider using character class brackets instead. This can often make things more readable, as well as being more proper regex syntax.

Code:

sed '\|[.]/[.]|d' file
Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

A couple of regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html


All times are GMT -5. The time now is 04:55 PM.