10

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

com.blendtuts/S
°=
com.blengineering.www/:http
±=

I want to have the output :

com.blendtuts/S
com.blengineering.www/:http

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

30

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
26

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

grep "\."  file > newfile

That way you can save your old file.

10

Or keep the lines which contain a dot,

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

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

$ awk -F\. 'NF>1' file
com.blendtuts/S
com.blengineering.www/:http

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 '.'. – Chris Midgley Nov 8 '16 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 '16 at 13:43
6

Using perl:

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

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:

:g!/\./d

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 \.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.