1

I have a file that prints out multiple occurrences of '200 OK'. I am only interested in the last occurrence of '200 OK' that comes after the pattern 'COMMAND'. In my particular file, "COMMAND" prints 100 time. It would look like this:

otherdata 200 OK otherdata 200 OK COMMAND 200 OK 

So I use the command.

grep -A1 COMMAND file | grep -v '200 OK' 

This give me the following output:

COMMAND xxxxx PASSWORD xxxxxx -- COMMAND xxxxx PASSWORD xxxxxx 513 unknown user account -- COMMAND xxxxxx PASSWORD xxxxxx -- COMMAND xxxxxx PASSWORD xxxxxx 513 unknown user account -- COMMAND xxxxxx PASSWORD xxxxxxx -- 

I am getting everything other than the 200 OK, but I do not need the lines that contain

COMMAND xxxxx PASSWORD xxxxx --- 

How can I get the output of only the lines with anything other than 200 OK and the line above it? Ideally, I want my output to include only lines like this and nothing else. The 513 is just an example there may be others.

COMMAND xxxxx PASSWORD xxxxxx 513 user account unknown 
1
  • 3
    Could you please post a relevant input sample and expected output ? Commented Sep 1, 2016 at 14:52

1 Answer 1

4

You could do this with sed:

sed -n -e '/COMMAND/{N;/200/! p;}' file 

This used -n to not print lines by default, then if a line matches COMMAND, join that line with the next line (N and if that does not match 200 print the combined lines.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.