2

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..

2 Answers 2

6

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)?

1
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .