Skip to content
Advertisement

How to join command output lines into by separating then with n character [closed]

I have below data which is just fetching the data which is 80 or more than 80%

# ssh udcl804 aggr show -root false | awk '/^udc/{if($4 >= 80) print $0}'
udc8003_ssd02 127.2TB 10.09TB  92% online     179 udc8003          raid_dp,
udc8004_ssd02 127.2TB 18.95TB  85% online     149 udc8004          raid_dp,
udc8013_sata02 160.1TB 16.30TB 90% online     325 udc8013          raid_dp,
udc8014_sata02 160.1TB 22.18TB 86% online     328 udc8014          raid_dp,
udc8015_sata02 160.1TB 18.32TB 89% online     316 udc8015          raid_dp,
udc8016_sata02 160.1TB 23.97TB 85% online     316 udc8016          raid_dp,
udc8017_ssd02 131.6TB 25.79TB  80% online     101 udc8017          raid_dp,
udc8018_ssd02 131.6TB 22.30TB  83% online      83 udc8018          raid_dp,
udc8020_sata02 199.4TB 17.46TB 91% online     117 udc8020          raid_dp,
udc8026_ssd02 130.4TB 21.32TB  84% online     104 udc8026          raid_dp,
udc8030_ssd02 130.9TB 18.48TB  86% online     129 udc8030          raid_dp,

I am just looking if we could all the lines into one by separating them by newline character n and the closing it within double quotes as this is required for the destination format.

"udc8003_ssd02 127.2TB 10.09TB  92% online     179 udc8003          raid_dp,nudc8004_ssd02 127.2TB 18.95TB  85% online     149 udc8004          raid_dp,nudc8013_sata02 160.1TB 16.30TB 90% online     325 udc8013          raid_dp,nudc8014_sata02 160.1TB 22.18TB 86% online     328 udc8014          raid_dp,nudc8015_sata02 160.1TB 18.32TB 89% online     316 udc8015          raid_dp,nudc8016_sata02 160.1TB 23.97TB 85% online     316 udc8016          raid_dp,nudc8017_ssd02 131.6TB 25.79TB  80% online     101 udc8017          raid_dp,nudc8018_ssd02 131.6TB 22.30TB  83% online      83 udc8018          raid_dp,nudc8020_sata02 199.4TB 17.46TB 91% online     117 udc8020          raid_dp,udc8026_ssd02 130.4TB 21.32TB  84% online     104 udc8026          raid_dp,nudc8030_ssd02 130.9TB 18.48TB  86% online     129 udc8030         raid_dp,"

This is just in continuation of post.

Advertisement

Answer

Use printf() instead of print, as it doesn’t automatically add a newline. Then you can put a literal n in the output. I’ve put this in a variable so I don’t put it before the first line, only all subsequent lines.

Use BEGIN and END blocks to print quotes around it.

ssh udcl804 aggr show -root false | awk 'BEGIN {printf """; prefix=""}
    /^udc/{if($4 >= 80) printf("%s%s", prefix, $0); prefix="\n"}
    END {printf """}'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement