Skip to main content

awk and sed.

Another of my subjects this semester at uni, had me using a bit of awk and sed this week just gone, and I must say, that I now do have a more understanding knowledge of both of these tools.

Before I did understand how to do rather small things with each (like change instances of words, to another word, and pull out every 4th word from a sentence to use elsewhere), I never did try and go any more into either of them.

As you can see from the following code snippets, I think I have dug a bit more into these tools.

sed: Change the order, of the first two parameters called by a function

s/somefunc((.*),(.*)(,.*))/somefunc(2,13)/g

awk: add line numbers to each line of a source file, without skipping numbers. Comments should not be numbered.

{ if ($1 ~////) { printf("%5st%sn", "", $0); next } } { if ($1 ~//*/) { z = 1; printf("%5st%sn", "", $0); next } } { if ($1 ~/*//) { z = 0; printf("%5st%sn", "", $0); next } } { if (z > 0) { printf("%5st%sn", "", $0); next } } { printf("%5st%sn", (NF? ++a ":" :""), $0) }

A sample output for this awk script would be:

1: #include <stdio.h>

// main routine

System Message: WARNING/2 (<string>, line 24)

Block quote ends without a blank line; unexpected unindent.

2: int main(int argc, char *argv[]) 3: {

System Message: WARNING/2 (<string>, line 24); backlink

Inline emphasis start-string without end-string.

System Message: ERROR/3 (<string>, line 26)

Unexpected indentation.
/* Incredibly complex function here --
  • it prints hello world

System Message: WARNING/2 (<string>, line 28)

Bullet list ends without a blank line; unexpected unindent.

*/

System Message: WARNING/2 (<string>, line 28); backlink

Inline emphasis start-string without end-string.

System Message: WARNING/2 (<string>, line 29)

Block quote ends without a blank line; unexpected unindent.

4: printf("%s", "Hello, world"); 5: return 0; // return zero! 6: }

And comparing what I did, to some other features of these tools, this is still only the beginning of their abilities.