Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2006
    Posts
    8

    Question Unanswered: Format results of grep

    Hi

    I have a shell scrpt that performs grep and the results are displayed in one long column for example:

    a
    b
    c
    d
    e
    f
    g
    h
    i
    j
    k
    l

    This is presented to the users and what I would like to do is format this so it appears in multiple columns for example:

    a b c d e
    f g h i j
    k l


    Any ideas PLEASE !
    Thanks

  2. #2
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    248
    Pipe the 'grep' output to 'awk', like this:
    Code:
    grep 'echo' gettab | awk '{printf("%s ",$0)}END{print}'
    This command does a grep for 'echo' in the file 'gettab'. The output will be captured by awk which will print every inputline ($0) as a string to stdout without a newline (\n), which otherwise had to be explicitly defined as "%s\n" in printf. The END section will eventually append a newline to conclude the output of the command.

    BTW: The printf function also exists in the schell commandset but I couldn't find out how to pass the grep outputline to it as an argument.

    Regards

  3. #3
    Join Date
    Aug 2006
    Posts
    8
    Please see response below
    Last edited by mattlucas; 08-16-06 at 09:18.

  4. #4
    Join Date
    Aug 2006
    Posts
    8
    Thanks for response and I am nearly there now. The only problem is that it returns:

    Code:
    a      b      c
       d      e      f
    g      h      i
          j      k      l      m
    is it possible to get them

    Code:
    a b c
    d e f
    g h i
    j k h

  5. #5
    Join Date
    Aug 2006
    Posts
    8
    Thanks for help on this I had to change the code a little so it now looks like this

    Code:
     awk '{printf("%s",$0)}END{print}'
    without the space after the %s

    Thanks once again for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •