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 ?
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:
The dollar sign signifies that the match must be at the end of the line.
This User Gave Thanks to Scrutinizer For This Post:
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:-
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?
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:
This User Gave Thanks to Scrutinizer For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)