Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux: remove last comma

Posted on 2013-11-15
3
Medium Priority
?
307 Views
Last Modified: 2013-11-15
The very last item should not end in a comma:
df | awk '! /[Ff]ilesystem/ {sub("%","",$5); print"\""$1"\":"$5"," }'| sed 's/^,*//g'

Open in new window

0
Comment
Question by:hankknight
3 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 39651139
I can't be totally sure what you are doing, but if you are using

sed 's/^,*//g'

that will remove a leading comma, but not a trailing comma. Try

sed 's/,*$//g'
0
 
LVL 16

Author Comment

by:hankknight
ID: 39651145
That removes ALL commas.  I need valid JSON so I only want the very last comma to be removed.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 39651188
df  | awk '! /[Ff]ilesystem/ {sub("%","",$5); print "\""$1"\":"$5"," }'| sed '$s/,$//'
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month8 days, 4 hours left to enroll

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question