1

I have some output that looks like the following:

Gathering data for drive 1 ...
Drive name:  id1,sd@n5000cca17096
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp:  41 C

Gathering data for drive 2 ...
Drive name:  id1,sd@n5000cca24156
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp:  41 C

Gathering data for drive 3 ...
Drive name:  id1,sd@n5000cca8749
Drive Model: HUH721010AL4204
failed to get drive stats

Gathering data for drive 4 ...
Drive name:  id1,sd@n5000cca19183
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp:  41 C

Gathering data for drive 5 ...
Drive name:  id1,sd@n5000cca4607
Drive Model: HUSMH8010BSS204
failed to get drive stats

Gathering data for drive 6 ...
Drive name:  id1,sd@n5000cca10152
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp:  41 C

I would like to print the "Drive name" for any drive in which the drive stats cannot be returned (so matching on failed to get drive stats).

I know how to get the previous line with:

awk '$0=="failed to get drive stats" && $2>1 {print f} {f=$0}'

But that doesn't help me.

Desired output:

Drive name:  id1,sd@n5000cca8749
Drive name:  id1,sd@n5000cca4607

Edit:

For some reason when the output is redirected it doesn't appear the same way as it does on the terminal. If I run:

command >out.txt 2>&1

The output file will look like:

failed to get drive stats
failed to get drive stats

Gathering data for drive 1 ...
Drive name:  id1,sd@n5000cca17096
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp:  41 C

Gathering data for drive 2 ...
Drive name:  id1,sd@n5000cca24156
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp:  41 C

....

Both awk answers provided work but I just have other issues I guess

4
$ awk -F ':' '$1 == "Drive name" { saved = $0 } $0 == "failed to get drive stats" { print saved }' file
Drive name:  id1,sd@n5000cca8749
Drive name:  id1,sd@n5000cca4607

This first sets the input field separator to a :. It then saves the line in the variable saved whenever a line specifying a drive name is found.

If a line says failed to get drive stats, the current value of saved is printed.

2
  • It's so strange because if I save the command output to a file this works but trying to read it through a pipe and it doesn't. So it has nothing to do with the awk version – jesse_b Jan 16 '20 at 22:59
  • stderr is unbuffered but stdout is buffered - maybe? – NickD Jan 16 '20 at 23:28
0

This is why awk has a "paragraph mode", all you need is:

$ awk -v RS= -F'\n' '/failed to get drive stats/{print $2}' file
Drive name:  id1,sd@n5000cca8749
Drive name:  id1,sd@n5000cca4607
0

Tried with Below method and it worked fine

awk '{a[++i]=$0}/failed/{for(x=NR-2;x<NR;x++)print a[x]}' filename| sed -n '1~2p'

output

Drive name:  id1,sd@n5000cca8749
Drive name:  id1,sd@n5000cca4607
1
  • Ask yourself what the difference is between i and NR. Now ask yourself why you need i. Now ask yourself why you're printing 2 lines at a time with a loop in awk and then deleting the 2nd line of every pair with sed. You also never need sed when you're using awk anyway. – Ed Morton Jan 18 '20 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.