So far, I have this:

grep -B 36 'pattern1' Mapper.1.Configuration.MapRules | grep "pattern2" | cut -d'"' -f2

which give me a new-line delimited list of values that I need. I could put this into a file and then do a find and replace of all new-lines with commas.

But I'm guessing (nay, hoping!) there's a way to do this on the command line?

  • why not use grep or awk?
    – alexus
    Mar 14, 2014 at 15:09

1 Answer 1


A quick search shows the following answer from the Stack Exchange universe which should be applicable:

<command> | tr "\\n" ","


  • Thanks, for this. I was having trouble finding the right text to search on. This seems to work except that it's also taking the next command prompt as input...it seems. i.e. the last value has a comma after it but also has "root@ip-address:/current-dir" appended to it. This is a great lead though. i'll look into tr.
    – Ramy
    Mar 14, 2014 at 15:00
  • It doesn't have the next prompt as input. The \n at the very end of the output is replaced by a comma, so the prompt printed by your terminal after the command completes doesn't go onto a new line.
    – USD Matt
    Mar 14, 2014 at 15:02
  • ah. of course. I'll keep playing with it to get what i need.
    – Ramy
    Mar 14, 2014 at 15:04
  • There's various extra bits on information on the stackoverflow question. You could also try appending | sed 's/,$//'. That removes the last comma and adds a newline for me although I am using FreeBSD so your output may vary slightly from mine.
    – USD Matt
    Mar 14, 2014 at 15:07
  • thanks, for the tip. that sed command didn't work for me. I just removed the last character of the string like this: ${i%?} where i stores the results of my pipeline (that ends with the tr command)
    – Ramy
    Mar 14, 2014 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.