Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sed join lines

35 views
Skip to first unread message

cla...@gmail.com

unread,
Jan 20, 2015, 10:11:08 AM1/20/15
to
Hi

I have 5 lines like below, which I like to join into one.

top - 13:49:55 up 402 days, 22:38, 4 users, load average: 1.69, 1.58, 1.79
Tasks: 397 total, 4 running, 393 sleeping, 0 stopped, 0 zombie
Cpu(s): 4.0%us, 0.8%sy, 0.0%ni, 95.0%id, 0.2%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 15950M total, 15155M used, 795M free, 77M buffers
Swap: 2055M total, 101M used, 1953M free, 10928M cached

the joined output should look like this:
top - 13:49:55 up 402 days, 22:38, 4 users, load average: 1.69, 1.58, 1.79 Tasks: : 397 total, 4 running, 393 sleeping, 0 stopped, 0 zombie Cpu(s): 4.0%us, 0.8%sy, 0.0%ni, 95.0%id, 0.2%wa, 0.0%hi, 0.1%si, 0.0%st Mem: 15950M total, 15155M used, 795M free, 77M buffers Swap: 2055M total, 101M used, 1953M free, 10928M cached.
(join line before Tasks, Cpu(s), Mem and Swap)

But how do I do this using the sed command?

Thx. Claus

Kaz Kylheku

unread,
Jan 20, 2015, 12:35:20 PM1/20/15
to
On 2015-01-20, cla...@gmail.com <cla...@gmail.com> wrote:
> Hi
>
> I have 5 lines like below, which I like to join into one.
>
> top - 13:49:55 up 402 days, 22:38, 4 users, load average: 1.69, 1.58, 1.79
> Tasks: 397 total, 4 running, 393 sleeping, 0 stopped, 0 zombie
> Cpu(s): 4.0%us, 0.8%sy, 0.0%ni, 95.0%id, 0.2%wa, 0.0%hi, 0.1%si, 0.0%st
> Mem: 15950M total, 15155M used, 795M free, 77M buffers

> Swap: 2055M total, 101M used, 1953M free, 10928M cached
[ ... ]

> But how do I do this using the sed command?

At the shell prompt: join five lines from standard input onto standard output:

$ ( read l1; read l2; read l3; read l4; read l5;
printf "%s%s%s%s%s\n" "$l1" "$l2" "$l3" "$l4" "$l5" )
one
two
three
four
five
onetwothreefourfive

Use the correct newsgroup next time; sed is not topical in comp.lang.awk. It
is anti-topical, in a way. The space of problems for which sed is ideally
suited, even in comparison with awk, is small.

Kenny McCormack

unread,
Jan 20, 2015, 12:50:41 PM1/20/15
to
In article <932d0b39-eaa1-443f...@googlegroups.com>,
Don't use sed. As Kaz notes, sed is both off-topic and almost never the
right tool. It is the consensus of this newsgroup that once you get beyond
the very simple and strightforward: s/foo/bar/p
command line, sed is not the tool to use.

The AWK idiom is:

ORS=NR%5?" ":"\n"

(Yes, that's the whole program!)

This says to print every line, and the record terminator (somewhat
mislabeled in AWK terminology as "record separator") is a space unless the
line number is a multiple of 5 (in which case, the terminator is a
newline).

Also note: The above works because we are using the default action of
"print $0", and that default action will be executed whenever the condition
part is true - which is always, since the above expression always returns
either a space or a newline (both of which are "true" in AWK's mindset).

--
"Insisting on perfect safety is for people who don't have the balls to live
in the real world."

- Mary Shafer, NASA Ames Dryden -

Kaz Kylheku

unread,
Jan 20, 2015, 2:13:24 PM1/20/15
to
On 2015-01-20, Kenny McCormack <gaz...@shell.xmission.com> wrote:
> In article <932d0b39-eaa1-443f...@googlegroups.com>,
> <cla...@gmail.com> wrote:
>>Hi
>>
>>I have 5 lines like below, which I like to join into one.
>>
>>top - 13:49:55 up 402 days, 22:38, 4 users, load average: 1.69, 1.58, 1.79
>>Tasks: 397 total, 4 running, 393 sleeping, 0 stopped, 0 zombie
>>Cpu(s): 4.0%us, 0.8%sy, 0.0%ni, 95.0%id, 0.2%wa, 0.0%hi, 0.1%si, 0.0%st
>>Mem: 15950M total, 15155M used, 795M free, 77M buffers
>>Swap: 2055M total, 101M used, 1953M free, 10928M cached
>>
>>the joined output should look like this:
>>top - 13:49:55 up 402 days, 22:38, 4 users, load average: 1.69, 1.58, 1.79
>>Tasks: : 397 total, 4 running, 393 sleeping, 0 stopped, 0 zombie Cpu(s):
>>4.0%us, 0.8%sy, 0.0%ni, 95.0%id, 0.2%wa, 0.0%hi, 0.1%si, 0.0%st Mem:
>>15950M total, 15155M used, 795M free, 77M buffers Swap: 2055M
>>total, 101M used, 1953M free, 10928M cached.
>>(join line before Tasks, Cpu(s), Mem and Swap)
>>
>>But how do I do this using the sed command?
>
> Don't use sed. As Kaz notes, sed is both off-topic and almost never the
> right tool.

Moreover, it should almost never be the right tool in the sense that it is
available, whereas awk isn't.

Anything that calls itself POSIX, and even things that call
themselves pre-POSIX Unixes, will almost certainly not have one without the
other.

However, in a situation where sed has to be used because awk isn't available
(because, for instance, someone hacked down some *nix distro to fit into a
small EEPROM memory or whatever, and there was no room for awk),
comp.lang.awk would then obviously be the right newsgroup to ask.

It's a Usenet rule! If you can't use X because it is not available, then you
must ask for a solution specifically in comp.lang.X.

This is topical because your solution is being developed in the specific
emotional context of *wishing* that you had X, and you want to reduce your
distress by sharing your feeling with other X programmers. Secondly, the
solution will use Y as if it were X, using X-like idioms in Y even if they are
inappropriate! Lastly, the archived discussion might be of value to any
X programmer who lands in the same predicament.
0 new messages