user2023
user2023

Reputation: 478

How to add the double quote at the very first and at the end of last line

I am looking to add double quote at the very first line and at the of the last line in a file.

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,

My trial:

I tried below but this adds double quote at the beggining of each line only.

# ssh udcl804 aggr show -root false | awk '/^udc/{if($4 >= 80) print $0}' | sed 's/^/"/;s/$/"/;'
"udc8003_ssd02 127.2TB 10.11TB  92% online     179 udc8003          raid_dp,
"udc8004_ssd02 127.2TB 18.96TB  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.33TB 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 need the data to like " .. all lines" but each needs be separated as a new line while POSTING them as a json.

Edit:

I am just looking if we could all the lines into one by separating them by newline character \n 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,"

Upvotes: 0

Views: 106

Answers (3)

anubhava
anubhava

Reputation: 785176

You may use this single awk to get the job done:

ssh udcl804 aggr show -root false |
awk '/^udc/ && $4 >= 80 {sub(/\r$/, ""); s = (s == "" ? "" : s "\\n") $0} END {printf "\"%s\"\n", s}'

"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,\nudc8026_ssd02 130.4TB 21.32TB  84% online     104 udc8026          raid_dp,\nudc8030_ssd02 130.9TB 18.48TB  86% online     129 udc8030          raid_dp,"

Explanation:

  • /^udc/ && $4 >= 80: For these 2 conditions only
  • {sub(/\r$/, "");: Remove trailing \r from all lines
  • s = (s == "" ? "" : s "\\n") $0: Keep saving each line in variable s with \n as line delimiter
  • END {printf "\"%s\"\n", s}: Print buffer with " on both sides.

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84561

Why not simply:

# ssh udcl804 aggr show -root false | 
awk 'FNR==1{printf "\"%s",$0; next} {printf "%s",$0} END {print "\""}'

(which simply appends each line and wraps the entire output from the ssh command in double quotes)

Upvotes: 2

tripleee
tripleee

Reputation: 189397

In the general case,

sed -e '1s/^/"/' -e '$s/$/"/'

adds a double quote to the beginning of the first line, and another to the end of the last.

However, if you are using Awk anyway, probably add the functionality to your existing Awk script instead.

awk 'BEGIN { q = "\042" }
    /^udc/ && $4 >= 80 { printf "%s%s", q, $0; q="\n" }
    END { printf "\042\n" }'

If you want to output the literal string \n instead of a proper newline, change \n to \\n, where \\ produces a literal backslash in a quoted string in Awk.

Upvotes: 1

Related Questions