简体   繁体   中英

GREP - Searching for specific string going backwards

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 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM