nullis.net

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 

    // main routine

2: int main(int argc, char argv[]) 3: { / Incredibly complex function here -- * it prints hello world */ 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.

Comments

Wednesday, 22 November 2006 @ 1:47 a.m.

(#1) — Kent Larsson

Your sed command doesn't work for me (I suspect you have some errors in there). However this method works:

$ echo "somefunc(al,be,ze)" | sed -e 's/somefunc(\([^,]*\),\([^,]*\),\(.*\)/somefunc(\2,\1,\3)/g'
somefunc(be,al,ze))

Add a Comment

Not currently logged in.   Login.   Register?