I have a file that contains data including urls. But there are various lines which are not urls. How can I remove them using Ubuntu terminal commands?

Here is the Sample file for reference: Sample Data


I want to have the output :


The extra unwanted lines do not have any dot. Hence, I want to remove the lines without dots

6 Answers 6


One way with sed

sed '/\./!d' file
  • /\./ match literal dot (escaped with \ because otherwise. matches any character)
  • !d delete everything except the matched pattern

If you want to edit the file in place, add -i to the command after testing. (You can also add .bak to the -i flag sed -i.bak ... to create a local backup of the file.)

sed -i '/\./!d' file

You could grep everything with a dot into a new file:

grep "\."  file > newfile

That way you can save your old file.


Or keep the lines which contain a dot,

sed -ni.bak '/\./p' infile

I think awk is that last one missing to the party:

$ awk -F\. 'NF>1' file

This sets the field separator to the dot. Then, it is a matter of printing those lines that have at least two fields: this will mean that at least one dot occurs.

  • 3
    A simpler awk solution would be to do it in the same vein as grep: awk '/\./' file -- that is, to print only the lines containing the character '.'. Nov 8, 2016 at 13:05
  • @ChrisMidgley good one! Since this is what the answer with grep already suggested, I preferred to go with some awk-specific stuff :)
    – fedorqui
    Nov 8, 2016 at 13:43

Using perl:

perl -i -ne 'print if /\./' /path/to/file

You can do this pretty easily with vim. If you are comfortable using vim as a text editor (opening, editing, and writing files), then do this:


If you are not comfortable using vim, or you would rather use it as a command-line tool, you may simply do from the terminal:

vim file -c "g!/\./d" -c "wq"

You could slightly modify this to save to a new file:

vim file -c "g!/\./d" -c "w newfile" -c "q!"

This uses vim's "global" command, which applies an ex command to every line matching (or not matching) a regex. In this case, the command is "(d)elete", and it will be applied to every command not matching the regex \.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .