Text processing is fun. Well, fun if you like to beat your head against a wall. Most of the time I just string a few things together to get whatever I am doing done. Its much better to find the shortest way to do something, it spawns less processes, is more efficient and generally a good idea. So if you have ever wanted to match a string from some given output and only print the preceding line here you go.

awk '/pattern/{print x};{x=$0}'

You might also wonder why you might want to do that. Well the reason I wanted to do it was to pull the sar output of the last run. Sar output ends in ^Average: but I just wanted the line before that so this is what I came up with.

sar | awk '/^Average:/{print x};{x=$0}'

That just spits out the last recorded sar information like this.

`Text processing is fun. Well, fun if you like to beat your head against a wall. Most of the time I just string a few things together to get whatever I am doing done. Its much better to find the shortest way to do something, it spawns less processes, is more efficient and generally a good idea. So if you have ever wanted to match a string from some given output and only print the preceding line here you go.

awk '/pattern/{print x};{x=$0}'

You might also wonder why you might want to do that. Well the reason I wanted to do it was to pull the sar output of the last run. Sar output ends in ^Average: but I just wanted the line before that so this is what I came up with.

sar | awk '/^Average:/{print x};{x=$0}'

That just spits out the last recorded sar information like this.

`

Great for using in scripts to collect bits of information you might need.

Oh and for good measure if I wanted to do this before I would have done this.

sar | grep -B1 ^Average: | grep -v ^Average

And look at how much time I saved by not having the extra pipe!

``Text processing is fun. Well, fun if you like to beat your head against a wall. Most of the time I just string a few things together to get whatever I am doing done. Its much better to find the shortest way to do something, it spawns less processes, is more efficient and generally a good idea. So if you have ever wanted to match a string from some given output and only print the preceding line here you go.

awk '/pattern/{print x};{x=$0}'

You might also wonder why you might want to do that. Well the reason I wanted to do it was to pull the sar output of the last run. Sar output ends in ^Average: but I just wanted the line before that so this is what I came up with.

sar | awk '/^Average:/{print x};{x=$0}'

That just spits out the last recorded sar information like this.

`Text processing is fun. Well, fun if you like to beat your head against a wall. Most of the time I just string a few things together to get whatever I am doing done. Its much better to find the shortest way to do something, it spawns less processes, is more efficient and generally a good idea. So if you have ever wanted to match a string from some given output and only print the preceding line here you go.

awk '/pattern/{print x};{x=$0}'

You might also wonder why you might want to do that. Well the reason I wanted to do it was to pull the sar output of the last run. Sar output ends in ^Average: but I just wanted the line before that so this is what I came up with.

sar | awk '/^Average:/{print x};{x=$0}'

That just spits out the last recorded sar information like this.

`

Great for using in scripts to collect bits of information you might need.

Oh and for good measure if I wanted to do this before I would have done this.

sar | grep -B1 ^Average: | grep -v ^Average

And look at how much time I saved by not having the extra pipe!

``

vs

```Text processing is fun. Well, fun if you like to beat your head against a wall. Most of the time I just string a few things together to get whatever I am doing done. Its much better to find the shortest way to do something, it spawns less processes, is more efficient and generally a good idea. So if you have ever wanted to match a string from some given output and only print the preceding line here you go.

awk '/pattern/{print x};{x=$0}'

You might also wonder why you might want to do that. Well the reason I wanted to do it was to pull the sar output of the last run. Sar output ends in ^Average: but I just wanted the line before that so this is what I came up with.

sar | awk '/^Average:/{print x};{x=$0}'

That just spits out the last recorded sar information like this.

`Text processing is fun. Well, fun if you like to beat your head against a wall. Most of the time I just string a few things together to get whatever I am doing done. Its much better to find the shortest way to do something, it spawns less processes, is more efficient and generally a good idea. So if you have ever wanted to match a string from some given output and only print the preceding line here you go.

awk '/pattern/{print x};{x=$0}'

You might also wonder why you might want to do that. Well the reason I wanted to do it was to pull the sar output of the last run. Sar output ends in ^Average: but I just wanted the line before that so this is what I came up with.

sar | awk '/^Average:/{print x};{x=$0}'

That just spits out the last recorded sar information like this.

`

Great for using in scripts to collect bits of information you might need.

Oh and for good measure if I wanted to do this before I would have done this.

sar | grep -B1 ^Average: | grep -v ^Average

And look at how much time I saved by not having the extra pipe!

``Text processing is fun. Well, fun if you like to beat your head against a wall. Most of the time I just string a few things together to get whatever I am doing done. Its much better to find the shortest way to do something, it spawns less processes, is more efficient and generally a good idea. So if you have ever wanted to match a string from some given output and only print the preceding line here you go.

awk '/pattern/{print x};{x=$0}'

You might also wonder why you might want to do that. Well the reason I wanted to do it was to pull the sar output of the last run. Sar output ends in ^Average: but I just wanted the line before that so this is what I came up with.

sar | awk '/^Average:/{print x};{x=$0}'

That just spits out the last recorded sar information like this.

`Text processing is fun. Well, fun if you like to beat your head against a wall. Most of the time I just string a few things together to get whatever I am doing done. Its much better to find the shortest way to do something, it spawns less processes, is more efficient and generally a good idea. So if you have ever wanted to match a string from some given output and only print the preceding line here you go.

awk '/pattern/{print x};{x=$0}'

You might also wonder why you might want to do that. Well the reason I wanted to do it was to pull the sar output of the last run. Sar output ends in ^Average: but I just wanted the line before that so this is what I came up with.

sar | awk '/^Average:/{print x};{x=$0}'

That just spits out the last recorded sar information like this.

`

Great for using in scripts to collect bits of information you might need.

Oh and for good measure if I wanted to do this before I would have done this.

sar | grep -B1 ^Average: | grep -v ^Average

And look at how much time I saved by not having the extra pipe!

``

vs

```