Monday, September 10, 2012

awk Script To Combine Lines Of A File or Output

I ran into a unique problem where I needed to combine 3 lines of numbers into a single line separated by spaces (ironically again, to use awk to generate C# code that does something interesting). I found posts that gave me an idea for how to accomplish the task; however the examples that I found don't cover what to do if there are no indications of the next "record" to parse (i.e. some sort of starting delimiter):

BEGIN {
   i=0
   l=""
}
{
   if (i == 3) {
      i=1
      print l
      l=$0
   }
   else {
      if (i == 0) {
         l=$0
      }
      else {
         l=l" "$0
      }
      i=i+1
   }
}
END {print l}

This script can be generalized to combine N lines of output by replacing the line:  
if (i == 3) {

with

if (i == N) {

where N is a variable or the desired number of lines to combine. This can eliminate a tedious job like deleting newlines from a file (particularly when copying some PDF tables into a text editor like notepad, emacs, or vi) and make a task part of a larger and more automated process.  Replacing $0 with $1..$n will only concatenate the nth field. This is easy to run on UNIX/Linux (since awk is included with most distributions), but on Windows it requires Cygwin (which I use) or a native port of the GNU awk utility (check out the GNUWin32 project on SourceForge).

No comments:

Post a Comment