Grep all lines with the pattern .sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep all lines with the pattern .sh
# 1  
Old 08-01-2017
Grep all lines with the pattern .sh

Linux version : Oracle Linux 6.5
Shell : bash


In the the below text file (someString.text), I want to grep all lines with .sh in it. ie. Only the lines mysript.sh and anotherscript.sh should be returned.
My below attempts failed.
I gather that in regular expression world, dot (.) is the wild card for any single character . So, what is the workaround ?


Code:
$ cat someString.text
/bin/sh
/bin/ssh
korn shell
mysript.sh
anotherscript.sh



$ grep *.sh someString.text
$

$ grep sh someString.text
/bin/sh
/bin/ssh
korn shell
mysript.sh
anotherscript.sh
$
$
$ grep .sh someString.text
/bin/sh
/bin/ssh
korn shell
mysript.sh
anotherscript.sh
$
$
$ grep *sh someString.text
$
$ grep sh someString.text
/bin/sh
/bin/ssh
korn shell
mysript.sh
anotherscript.sh
$

# 2  
Old 08-01-2017
The grep regular expression must be quoted. The dot has a special meaning ("any character") so it must be escaped with a back slash to use its literal meaning. try:
Code:
grep '\.sh$' file

The dollar sign signifies that the match must be at the end of the line.
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-01-2017
It might be better to quote the string you are searching for. The version grep *.sh someString.text could yield different results based on other files in the directory because the shell will assum you want to expand the * character to match all files that finish sh, so your command executed could end up being:-
Code:
grep 1sh 2.sh hello.sh my-oh-my.ksh wish someString.text

This would mean it would search for 1sh in all the other files listed, which probably is not what you want.

Additionally, the dot is a special character that matches any single character. That might seem odd, but you might want to match A..B to get A01B, A02B, A17B etc. so to ensure it is the literal character dot, you need to use a backslash \ to escape it.

With quoting, it might help a little, but you need to be clear on what you are searching for, so:-
  • If you are looking for the literal string .sh then try grep '\.sh' someString.text
  • If you are looking for the literal string .sh at the end of the line, try grep '\.sh$' someString.text
In the latter, the $ in this case marks/anchors the search to the end of line. There could be other cases, such as .sh followed by a space, tab, hash etc., depending on the whole content of your file.


Does this do what you need, or are there other conditions to consider?


Kind regards,
Robin

Last edited by rbatte1; 08-01-2017 at 06:13 AM..
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 08-01-2017
Another option is to use a literal string match (so the are no special meaning to characters) with the -F parameter. If .sh always only occurs at the end of the line anyway, you could also try:

Code:
grep -F '.sh' file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find pattern; grep n lines before and after

Hi, I need help to grep a specific part of a log file (bold). 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: } 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: extensionContainer <Not Present> 24/2/2017-16:57:17.056... (8 Replies)
Discussion started by: vasil
8 Replies

2. Shell Programming and Scripting

Grep pattern only and surrounding lines

Hello, I am trying to grep search a pattern and a line before it. cat input >record1 hello1hello2hellonhello3 >record2 helloohello1hello2hello3 When I use, grep with -o option and either of -A/B/C options, I still can't see lines before or after the pattern. But the exact pattern is... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

3. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

4. Shell Programming and Scripting

Grep lines before a pattern having some other pattern

Hi All, I am trying to fetch lines before a pattern, I got to know about -B flag in grep but we have to pass the number to get those lines before some pattern say (X), now what if I want to get line/s with some other pattern say (Y) before X pattern? How to get about it? please help. Input:... (5 Replies)
Discussion started by: dips_ag
5 Replies

5. Shell Programming and Scripting

Grep pattern and display all lines below

Hi I need to grep for a patter and display all lines below the pattern. For ex: say my file has the below lines file1 file2 file3 file4 file5 I NEED to grep for patter file3 and display all lines below the pattern. do we have an option to get this data. Let me know if you require... (5 Replies)
Discussion started by: venkidhadha
5 Replies

6. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

grep: get last 3 lines containing PATTERN from many files

Hi all, I am looking for a quick solution for this: I have many log files of an iterative program, and I would like to display the parameters of the last three iteration from each of those files. Relevant lines have the keyword: ITER I am using: tac ~/modeling*/fitting.log | grep -m 3 -e... (2 Replies)
Discussion started by: pnemeth
2 Replies

8. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

9. UNIX for Dummies Questions & Answers

Grep with 8 lines before and after pattern.

OK. I have a file I'd like to be able to grep, but on top of returning the line where the pattern matches, I'd like to be able to get the previous 8 lines and the following 8 lines. Is there a way to do this? (2 Replies)
Discussion started by: mrwatkin
2 Replies

10. Shell Programming and Scripting

grep to show lines only after pattern

When i grep for a pattern the search results comes up with matching lines(some before the pattern and some after)...how can i limit the search so that it shows only the lines after the pattern specified (5 Replies)
Discussion started by: wannalearn
5 Replies
Login or Register to Ask a Question