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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

<!--adsense-->

<pre class="brush: bash; title: ; notranslate" title="">awk '/pattern/{print x};{x=$0}'
</pre>

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.

<pre class="brush: bash; title: ; notranslate" title="">sar | awk '/^Average:/{print x};{x=$0}'
</pre>

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.<!--more-->

<!--adsense-->

<pre class="brush: bash; title: ; notranslate" title="">awk '/pattern/{print x};{x=$0}'
</pre>

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.

<pre class="brush: bash; title: ; notranslate" title="">sar | awk '/^Average:/{print x};{x=$0}'
</pre>

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.

<pre class="brush: bash; title: ; notranslate" title="">sar | grep -B1 ^Average: | grep -v ^Average
</pre>

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.<!--more-->

<!--adsense-->

<pre class="brush: bash; title: ; notranslate" title="">awk '/pattern/{print x};{x=$0}'
</pre>

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.

<pre class="brush: bash; title: ; notranslate" title="">sar | awk '/^Average:/{print x};{x=$0}'
</pre>

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.<!--more-->

<!--adsense-->

<pre class="brush: bash; title: ; notranslate" title="">awk '/pattern/{print x};{x=$0}'
</pre>

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.

<pre class="brush: bash; title: ; notranslate" title="">sar | awk '/^Average:/{print x};{x=$0}'
</pre>

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.

<pre class="brush: bash; title: ; notranslate" title="">sar | grep -B1 ^Average: | grep -v ^Average
</pre>

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