List lines ending with a period or semicolon

how can I write a command using grep or egrep to display all lines in a file ending with a semicolon ";" or period "." character.

+4
source share
5 answers

With grep (and egrep ), the dollar sign ( $ ) matches the end of the line, and carret ( ^ ) matches the start of the line. So, for example, if you want to combine strings containing exactly the word "fish", and you cannot use any other characters:

 grep '^fish$' 

It's important to use single quotes to wrap the search expression so that bash doesn't do anything funny for it.

So, to answer your question, you will need to use the search pattern '[.;]$' To match the character . or ; followed by the end of line character. I use this as an example of a test file:

 $ cat testfile one two; three. four: 

And here is the result:

 $ grep '[.;]$' testfile two; three. 

If you also want to allow spaces at the end of a line, use this pattern: '[.;][ \t]*$' , which will match any number of spaces or tabs after . or ; .

+8
source

This should do it:

 $ grep -E '(;|\.)$' 

The -E switch allows you to use regex mode. The expression simply matches a line ending with a semicolon or period.

Note. I have not tested this.

+2
source

Without -E :

 grep '\.\|;$' filename 
+2
source

With sed:

 sed -rn '/(\.|;)$/p' 

With awk

 awk '/(\.|;)$/' 
+2
source

three ways: any of them worked for me.

one

 grep '[\.;]$' your_file 

two

 awk '/\.$/ || /;$/' your_file 

three:

 awk '/[\.;]$/' your_file 
+1
source

Source: https://habr.com/ru/post/1437573/


All Articles