GREP - Searching for specific string going backwards - linux

I would like to search file.txt with grep to locate a url ending with. ".doc". When it finds .doc, I want grep to go backwards and find "http://" at the begining of that string.
The output would be http://somesite.com/random-code-that-changes-daily/somefilename.doc
There is only 1 .doc url on this page, so multiple search results should not be an issue.
Please excuse, I am a novice. I did locate the answer at one time but search for 1 hour and can no longer find. I am willing to read and learn but I do not think I'm using the correct search terms for what I want to do. Thank you.

You can use regular expressions,
with the marker ^ you can indicate the start of the line you are looking for.
with the marker $ you can indicate the end of the line you are looking for.
then, you can do something like
grep '^http:\\' \ '.doc$' file.txt
or
grep '^http://\|.doc$' file.txt
or not using regular expressions but just a matching pattern with wildcards as #choroba suggested:
grep 'http://.*\.doc' file.txt

You can also search for http:// and print the line if it contains .doc somewhere after it:
grep 'http://.*\.doc' file.txt
If you want to only print the matching part, use the -o option (if your version of grep supports it).

Related

Simple grep command to search for a string not working on my ubuntu

I have to search for a string in a file like below using grep which is not working as expected.
It's just a simple search of the string, but not sure why it is not working
echo "Naizhu NZ1020 Lady Necklace Sexy Tcollarbone Chain Alloy PlatingSilver" | grep "Lady Necklace"
Can somebody help me here why it's not working, want to know the reason
The command grep will print the whole line matching the pattern.
So
echo "Naizhu NZ1020 Lady Necklace Sexy Tcollarbone Chain Alloy PlatingSilver" | grep "Lady Necklace"
will give you
Naizhu NZ1020 Lady Necklace Sexy Tcollarbone Chain Alloy PlatingSilver
You might use grep -o or --only-matching to
Print only the matched (non-empty) parts of a matching line, with each
such part on a separate output line.
and to get only
Lady Necklace
Within the comments of the question it was mentioned that a file is used for input. Since the encoding of that is unknown currently, you may also try use character classes
grep -o "Lady[[:space:]]Necklace"
Please see man grep for more options.
You should also have a look into your input file and if the words you like to lookup are in the same line and separated with a space and not with other not printable characters.

"grep -rnw": search for a string in all files

Related question: How do I find all files containing specific text on Linux?
I have been using the command mentioned in the answer of above question to search for string occurences in all files:
grep -rnw '/path/to/somewhere/' -e "pattern"
However lately I encountered a problem, shown in the following picture:
Looks like this command only recognizes strings that stand out as a word or something. How should I modify the command to improve my search result?
explainshell helpfully explains your command, and gives an excerpt from man grep:
-w, --word-regexp
Select only those lines containing matches that form whole words.
So just remove -w since that explicitly does what you don't want:
grep -rn '/path/to/somewhere/' -e "pattern"

How to grep specific *name* in subfolders in Linux?

To grep some string in all subfolders I can use:
grep -rl "test" /path
I would like to grep some specific string assuming that the first part and the second part of the string could be different (has got different begin and end) and is case insensitive.
I tried to do something like this, but it doesn't work:
grep -rli "*test*" /path
Could you please tell me how to do this?
To be clear I would like to find all files (grep) including subfolders, where there is specific string, like e.g.:
"*test*"
You should try grep accepting perl regex:
grep -irlP ".*test.*" /path
The .* means matching (zero or more times) any characters except newline. More about meta-characters here
You can use almost any regex, so it's a really flexible solution to match complex patterns. For instance, if you searched for entire words only:
grep -rlP "\b(test)\b" /path
Of course there are limitations. For instance, multiline search with this method can be difficult.
You can try like this using the "--include" option:
grep -rli --include="test*" /path
From the man page of grep
--include=GLOB
Search only files whose base name matches GLOB (using wildcard
matching as described under --exclude).
grep -ilr "string" path/to/subdir
Will search sub-strings as well. grep searches pattern - you don't need those asterisks (or dot asterisks as in my pre-edit answer).
If you want to search the exact word and not its presence in sub-strings, use -w option.

Terminal command to find lines containing a specific word?

I was just wondering what command i need to put into the terminal to read a text file, eliminate all lines that do not contain a certain keyword, and then print those lines onto a new file. for example, the keyword is "system". I want to be able to print all lines that contain system onto a new separate file. Thanks
grep is your friend.
For example, you can do:
grep system <filename> > systemlines.out
man grep and you can get additional useful info as well (ex: line numbers, 1+ lines prior, 1+lines after, negation - ie: all lines that do not contain grep, etc...)
If you are running Windows, you can either install cygwin or you can find a win32 binary for grep as well.
grep '\<system\>'
Will search for lines that contain the word system, and not system as a substring.
below grep command will solve ur problem
grep -i yourword filename1 > filename2
with -i for case insensitiveness
without -i for case sensitiveness
to learn how grep works on ur server ,refer to man page on ur server by the following command
man grep
grep "system" filename > new-filename
You might want to make it a bit cleverer to not include lines with words like "dysystemic", but it's a good place to start.

How to use grep with regex to find all the files containing a 'String'?

I am using SSH secure shell in Unix, I want to search file with some 'TEXT' or string
and that string is like 'PACKAGE', followed by '%PA%EVENTS%'('PA_PACKAGE %PA%EVENTS%')
this % i am assuming as in SQL*PLUS, means i a string PA can occur any where before EVENTS
and EVENTS can occur any where after PA,
I want to search those file which this REGEX matches.
I ma not sure exactly what you are hoping to match, but the following will print out the lines that contain the text "PACKAGE" followed but zero or more characters and then the text "EVENTS", from all the files in the current directory.
grep "PACKAGE.*EVENTS" *
Please give some more examples of what you want, so I can be more specific.
grep -l "PACKAGE*EVENTS" *
recursive:
grep -Rl "PACKAGE*EVENTS" *
try this: egrep -e "PACKAGE PA*EVENTS" file

Resources