1

I must find in a file this string:

200 https://www.example.example

The value 200 is randomic, I must find every HTTP return code (ex 200, 301, 404 etc...etc)

How can I grep only this string with return code variable (I don't want specify every return code in grep command)?

cat file.txt | grep "*** http*" 

But it doesn't work.

3
  • 1
    Not really sure what you are asking here. Try to read How to Ask and provide a minimal reproducible example so we can understand better. It is important to provide a relevant input and the desired output. Commented May 3, 2016 at 10:20
  • What about just taking the first 3 characters of each line? Commented May 3, 2016 at 10:28
  • grep "*** http*" is not close to the regexp you want. You should find some kind of regexp tutorial if you're going to be trying to use any tools that use regexps (grep, sed, awk, perl, ruby, etc....). Commented May 3, 2016 at 13:44

2 Answers 2

8

So you want to match any line starting with a three digit number, followed by "http"?

grep -E '^[0-9]{3} http' file.txt

More accurate as suggested by fedorqui (thanks) would be this:

grep -E '^[1-5][0-9]{2} http' file.txt

This matches numbers in the range 100-599, which is closer to the range of HTTP status codes.

Sign up to request clarification or add additional context in comments.

1 Comment

Probably ^[1-5][0-9]{2} is more accurate, since it is a HTTP status code.
0

First, there's no need for cat with grep. It can take a file argument itself.

You can do this:

grep '^[1-5][0-9]\{2\}[[:blank:]]\+http' file.txt

This will get you every line that matches with your criteria.

If you want only the return codes, you can run another grep or use other commands, for example with cut:

grep '^[1-5][0-9]\{2\}[[:blank:]]\+http' file.txt | cut -d ' ' -f1

and with grep,

grep '^[1-5][0-9]\{2\}[[:blank:]]\+http' file.txt | grep -o '^[0-9]\+'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.