Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to change the record separator but awk does not use the record separator I set until the second record and beyond.

I get output like this:

ARRAY /dev/md4 level=raid1 num-devices=2 metadata=0.90 UUID=e85beb4f:84b05929:786a31c9:93e269d4
ARRAY /dev/md6 level=raid1 num-devices=2 metadata=0.90 UUID=4dbc659a:4d11f1be:6cbe4d26:c1372c53
ARRAY /dev/md5 level=raid1 num-devices=2 metadata=0.90 UUID=7d34fc96:1fa6c3c5:b9932435:ad126815
ARRAY /dev/md3 level=raid1 num-devices=2 metadata=0.90 UUID=862ae8d0:bd00b2fd:9e171b97:2937fc49
ARRAY /dev/md2 level=raid1 num-devices=2 metadata=0.90 UUID=03bb1b77:42d474f4:608c5276:cc04d84c
ARRAY /dev/md1 level=raid1 num-devices=2 metadata=0.90 UUID=6c14cddc:3ef74d2d:2f26fa3f:32cc274b
ARRAY /dev/md0 level=raid1 num-devices=2 metadata=0.90 UUID=76e50525:9ae71e74:9697a0e6:6f77e132

I need output like this:

ARRAY=/dev/md4
level=raid1
num-devices=2
metadata=0.90
UUID=e85beb4f:84b05929:786a31c9:93e269d4
ARRAY=/dev/md6
level=raid1
num-devices=2
metadata=0.90
UUID=4dbc659a:4d11f1be:6cbe4d26:c1372c53
ARRAY=/dev/md5
level=raid1
num-devices=2
metadata=0.90
UUID=7d34fc96:1fa6c3c5:b9932435:ad126815
ARRAY=/dev/md3
level=raid1
num-devices=2
metadata=0.90
UUID=862ae8d0:bd00b2fd:9e171b97:2937fc49
ARRAY=/dev/md2
level=raid1
num-devices=2
metadata=0.90
UUID=03bb1b77:42d474f4:608c5276:cc04d84c
ARRAY=/dev/md1
level=raid1
num-devices=2
metadata=0.90
UUID=6c14cddc:3ef74d2d:2f26fa3f:32cc274b
ARRAY=/dev/md0
level=raid1
num-devices=2
metadata=0.90
UUID=76e50525:9ae71e74:9697a0e6:6f77e132

When I try to format it awk does nothing with the first record but then correctly formats the rest of the recors.

Here is the command I am using with the output:

$ mdadm --misc -sD |sed 's/ /=/' |awk '{RS=" "}{print $0}'
ARRAY=/dev/md4 level=raid1 num-devices=2 metadata=0.90 UUID=e85beb4f:84b05929:786a31c9:93e269d4
ARRAY=/dev/md6
level=raid1
num-devices=2
metadata=0.90
UUID=4dbc659a:4d11f1be:6cbe4d26:c1372c53
ARRAY=/dev/md5
level=raid1
num-devices=2
metadata=0.90
UUID=7d34fc96:1fa6c3c5:b9932435:ad126815
ARRAY=/dev/md3
level=raid1
num-devices=2
metadata=0.90
UUID=862ae8d0:bd00b2fd:9e171b97:2937fc49
ARRAY=/dev/md2
level=raid1
num-devices=2
metadata=0.90
UUID=03bb1b77:42d474f4:608c5276:cc04d84c
ARRAY=/dev/md1
level=raid1
num-devices=2
metadata=0.90
UUID=6c14cddc:3ef74d2d:2f26fa3f:32cc274b
ARRAY=/dev/md0
level=raid1
num-devices=2
metadata=0.90
UUID=76e50525:9ae71e74:9697a0e6:6f77e132

And I cannot use this command as it leaves off the array and I need that mdadm --misc -sDY

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Use sed twice, the second time to replace spaces with newlines:

mdadm --misc -sD | sed 's/ /=/'| sed 's/ /\n/g'

or slightly optimized, only once but with two patterns:

mdadm --misc -sD | sed 's/ /=/;s/ /\n/g'
share|improve this answer
    
perfect thank you talk about a head slap moment as soon as i saw your answer I just groaned and shook my head here is how I wrote before you edited your answer '$ mdadm --misc -sD |sed -e 's/ /=/' -e 's/ /\n/g'` I like your edit better Thanks –  menders65 Mar 13 at 7:50

I would also prefer the sed-only solution given by @SvW, but to answer your question:

The RS=" " comes too late. It is set after the first line is read. You should call it in a BEGIN block, for example, to have an effect also for the first line:

mdadm --misc -sD | sed 's/ /=/' | awk 'BEGIN{RS=" "}{print $0}'

Edit: Okay, @Thor was faster. :)

share|improve this answer
    
I tried what I thought was this method before asking the question but had a typo in it which I did not see until I just went back through my history. If you have two I's in BEGIN it does not work :-) –  menders65 Mar 13 at 11:28

Your solution does not work because RS is set too late in the process. Set it before awk receives any input and it works correctly:

mdadm --misc -sD | sed 's/ /=/' | awk '{print $0}' RS=' '

Perhaps the following awk-only answer is also useful:

mdadm --misc -sD | awk '{$2=$1"="$2; $1=""} 1' OFS='\n'
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.