Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a script which outputs (2 results shown here)

Tag: tag_453672
  Path: /abc/hosts/server42/vol/tempfile.tmp
  Server host: server42
  Region: unix
  Active: NO
  View tag uuid:00fa63f2.81f211e2.9f28.00:14:4f:9a:d2:60

Tag: tag_453672
  Path: /abc/hosts/server42/vol/tempfile.tmp
  Server host: server42
  Region: unix
  Active: NO
  View tag uuid:00fa63f2.81f211e2.9f28.00:14:4f:9a:d2:60

....  

and I use pipe greep to filter results:

script | grep "Tag\|Active"

which produce:

Tag: tag_453672
  Active: NO
Tag: tag_453672
  Active: YES

Is there a possibility to produce each filtered result on the same line like, eventually comma delimited?

Tag: tag_453672, Active: NO

Tag: tag_453672, Active: YES

share|improve this question

migrated from stackoverflow.com Apr 30 '13 at 15:22

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers

up vote 1 down vote accepted
script | grep "Tag\|Active" | awk 'NR%2==1{x=$0} NR%2==0{print x, $0}'

Stores whenever encountering an odd line, prints when even line. Can be made less crude by using regex instead of NR conditions.

Another approach with awk

script | awk '/^Tag/{x=$0} /^ *Active/{print x, $0}'
share|improve this answer
    
Thanks very much, first option fits best. Thanks all of you, I cannot give points for all :( –  user2274696 Apr 30 '13 at 12:11
add comment

Perl solution:

./script | grep 'Tag\|Active' | perl -pe '/^Tag:/ and chomp'
share|improve this answer
    
Also thank you. –  user2274696 Apr 30 '13 at 12:16
add comment

yass (yet another sed solution)

script | grep "Tag\|Active" | sed '$!N;s/\n/,/'

Other sed one-liners at http://www-rohan.sdsu.edu/doc/sed.html

share|improve this answer
    
Thanks, sed also work nice. –  user2274696 Apr 30 '13 at 12:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.