a bash commands outputs this:
Runtime Name: vmhba2:C0:T3:L14
Group State: active
Runtime Name: vmhba3:C0:T0:L14
Group State: active unoptimized
Runtime Name: vmhba2:C0:T1:L14
Group State: active unoptimized
Runtime Name: vmhba3:C0:T3:L14
Group State: active
Runtime Name: vmhba2:C0:T2:L14
Group State: active
I'd like to pipe it to something to make it look like this:
Runtime Name: vmhba2:C0:T1:L14 Group State: active
Runtime Name: vmhba3:C0:T3:L14 Group State: active unoptimized
Runtime Name: vmhba2:C0:T2:L14 Group State: active
[...]
i.e. remove every other newline
I tried ... |tr "\nGroup" " "
but it removed all newlines and ate up some other letters as well. thanks
can't test right now, but
should do it
One possibility is:
If the line number is evenly divisible by 2, end with a new line, otherwise, end with a space.
(Tested on: CentOS 6, GNU Awk 3.1.7)
Using sed (see explanation):
Further reading:
If you want to use
sed
, there's no reason to read the whole file into memory. You can merge every other line like this:Use any character you'd like instead of the space.
Here's another way using awk:
The
if/else
handles the case where there is an odd number of lines in the file. Without it, the odd last line gets printed twice. Otherwise, for comparison, you could do:The most idiomatic way to do it in
awk
is as follows:It outputs:
To explain it we need to define each one of the built-in variables:
RS
record separator. Defaults to\n
(new line).ORS
output record separator. Defaults to\n
(new line).FS
field separator. Defaults to(space).
NR
number of record.As the default record separator is the new line, a record is, as default, a line.
NR%2
is the modulus ofNR/2
, so that it will be either0
or1
.0
for even lines and1
for odd lines.var=condition?condition_if_true:condition_if_false
is the ternary operator.All together, saying
ORS=NR%2?FS:RS
we are defining the output record separator:2k + 1
, that is, on even lines, then the output record separators is set toFS
, that is, a space.2k
, that is, on odd lines, then the output record separators is set toRS
, that is, a new line.This way, odd lines end with a space, that is then joined with the next line. After that line, a new line is printed.
More info in Idiomatic awk.
This works for me on Linux:
This replaces an empty space for a newline character; you must escape the newline character for things to work correctly.
In bash:
If Perl is an option:
perl -pe 's/\n/ / if $. % 2 == 1' file
s/\n/ /
replaces newline with space$.
is the line numberHow about using
grep
?