If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Format results of grep

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-16-06, 07:42
mattlucas mattlucas is offline
Registered User
 
Join Date: Aug 2006
Posts: 8
Question 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
Reply With Quote
  #2 (permalink)  
Old 08-16-06, 08:02
Tyveleyn Tyveleyn is offline
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old 08-16-06, 08:15
mattlucas mattlucas is offline
Registered User
 
Join Date: Aug 2006
Posts: 8
Please see response below

Last edited by mattlucas; 08-16-06 at 08:18.
Reply With Quote
  #4 (permalink)  
Old 08-16-06, 08:17
mattlucas mattlucas is offline
Registered User
 
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
Reply With Quote
  #5 (permalink)  
Old 08-16-06, 08:23
mattlucas mattlucas is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On