-3

I have a text file with a list of domain names(each on a seperate line).
How do I remove the lines not ending in .org using bash or python?
This is what I have so far which kind of works but not fully. This list will then be used later in python.

grep .org output1.txt
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
AaronC
  • 15
  • 1
  • 5
  • 1
    Please, read the man page of `grep`. (Google will also help because some people put man pages on web sites.) `grep` has a specific option to reverse the pattern matching. – Scheff's Cat Feb 25 '17 at 13:44
  • grep -v .org output1.txt – AaronC Feb 25 '17 at 13:58
  • But how do I delete that output from the file? – AaronC Feb 25 '17 at 13:58
  • 1
    Maybe you need to look at the big picture. Is there really any point to deleting the data from the file when a future python script could easily ignore those lines in the file as it is processed? – grail Feb 25 '17 at 14:11
  • you write a new file, i.e. `grep '\.org' output1.txt > output1.org` If your determined to have only one file (test first!), then you can use `mv output1.org output1.txt` . Please read http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , http://stackoverflow.com/help/mcve and take the [tour](http://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Feb 25 '17 at 14:12
  • 1
    Possible duplicate of [Negative matching using grep (match lines that do not contain foo)](http://stackoverflow.com/questions/3548453/negative-matching-using-grep-match-lines-that-do-not-contain-foo) – Joe Feb 25 '17 at 14:40
  • 1
    @Joe, actually, this question is not even negative grep. It's just grep. "Remove lines not matching" == "Print lines that match"... – anishsane Feb 25 '17 at 15:08

3 Answers3

1
$ cat file
anything.org
anything.com
anything.org
anything.net
anything.com
anything.org
anything.net

$sed -i '/.org/!d' file

$cat file
anything.org
anything.org
anything.org
慕冬亮
  • 339
  • 1
  • 2
  • 10
  • 1
    This is a bit dangerous. It will keep lines such as `http://fundacionborges.com.ar`. For a safer command, you can use `sed -i -e '/\.org$/d!' file` – fzd Apr 06 '17 at 08:54
0

Perl one-liner

cat file
anything.org
anything.com
anything.org
anything.net
anything.com
anything.org
anything.net

perl -lne 'print if !/.*org/' file

anything.com
anything.net
anything.com
anything.net

Prints all lines that do not contain org at the end.
If you add -i it makes it edit in-place and delete that line.
perl -i -lne 'print if !/.*org/' file

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
0

The question apparently wants to remove the lines from the file.

ex file <<eof
g/[a-np-z][a-qs-z][a-fh-z]$/d
w
q
eof

will do that, assuming it is all lowercase.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31