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.

If I have:

foo  
bar

..and I'd like to awk/sed this to:

foo-bar

..what's the syntax?

I'm trying to use System Profiler on OS X 10.6.x:

system_profiler SPMemoryDataType | awk '/Type/ {print $2} /Speed/ {print $2}'

to output type-speed (ie DDR-1067). Haven't figured out how to do this using print or printf yet, might try to just grep instead..

share|improve this question

2 Answers 2

With tr:

tr '\n' '-'

Would that be enough? This replaces every newline with a comma.


With sed:

A prettier solution using sed:

sed -e N -e 's/\n/-/'

The N appends the newline to the pattern space according to the manpage:

[2addr]N

Append the next line of input to the pattern space, using an embedded newline character to separate the appended material from the original contents. Note that the current line number changes.


With bash

Actually, bash can do what tr can do. Just for completeness:

while read l; do echo -n "$l-"; done

Here's a similar question on Stack Overflow: SED: How can I replace a newline (\n)?

share|improve this answer
awk 'BEGIN {ORS="-"} {print}'

Joins your data. What's inside the begin statement happens prior to reading the data. Here we define that the output record separator is -. Print prints the data.

share|improve this answer

Your Answer

 
discard

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

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