2

Below string is in the format of name and value (' name' - 'value';). How to remove the occurrence of value '0 bytes' along with its name and remove the name '/platform/sun4v/lib/sparcv9/libc_psr.so.1' along with its value.

/devices - 0 bytes; /system/object - 0 bytes; /etc/dfs/sharetab - 0 bytes; /dev/fd - 0 bytes; /var/run - 13.77 GiB; /platform/sun4v/lib/sparcv9/libc_psr.so.1 - 27.24 GiB; / - 27.24 GiB; /var - 26.84 GiB; /proc - 0 bytes; /system/contract - 0 bytes; /rpool - 18.04 GiB; /mnt - 157.67 TiB; /vol - 0 bytes; /was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1 - 27.24 GiB; /etc/svc/volatile - 13.77 GiB; /etc/mnttab - 0 bytes; /tmp - 13.82 GiB;

4 Answers 4

3

Split the string on a selicolon and filter the array based on your criteria, then join the array to add back the semicolons and convert back to one string:

var str = '/devices - 0 bytes; /system/object - 0 bytes; /etc/dfs/sharetab - 0 bytes; /dev/fd - 0 bytes; /var/run - 13.77 GiB; /platform/sun4v/lib/sparcv9/libc_psr.so.1 - 27.24 GiB; / - 27.24 GiB; /var - 26.84 GiB; /proc - 0 bytes; /system/contract - 0 bytes; /rpool - 18.04 GiB; /mnt - 157.67 TiB; /vol - 0 bytes; /was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1 - 27.24 GiB; /etc/svc/volatile - 13.77 GiB; /etc/mnttab - 0 bytes; /tmp - 13.82 GiB;'

var splitStr = str.split(';');
var newStr = splitStr.filter(function(item){
         var itemSplit = item.split('-');

         return !(itemSplit != '' && (itemSplit[0].trim() ===  '/platform/sun4v/lib/sparcv9/libc_psr.so.1'  || itemSplit[1].trim() ==='0 bytes'));
}).join(';');

//newStr is will contain the filtered string
console.log(newStr);
Sign up to request clarification or add additional context in comments.

Comments

1

Try this.

str.split(';').filter(item =>  !item.includes('0 bytes') && !item.includes('/platform/sun4v/lib/sparcv9/libc_psr.so.1')).join(';')

Comments

1

You can use regular expressions here to transform the string.

See:

  1. The first regex (/\/platform\/sun4v\/lib\/sparcv9\/libc_psr\.so\.1 [^\;]+\;\s/) selects the long, unique substring;
  2. The second regex (/\/[^\-]+- 0 bytes\;\s/g) selects all instances of - 0 bytes (and each instance's preceding label)

var paragraphAfter = document.getElementsByClassName('after')[0];
var textBefore = document.getElementsByClassName('before')[0].textContent;
var textAfter = textBefore.replace(/\/platform\/sun4v\/lib\/sparcv9\/libc_psr\.so\.1 [^\;]+\;\s/,'');
var textAfter = textAfter.replace(/\/[^\-]+- 0 bytes\;\s/g,'');
paragraphAfter.textContent = textAfter;
<h2>Before:</h2>
<p class="before">/devices - 0 bytes; /system/object - 0 bytes; /etc/dfs/sharetab - 0 bytes; /dev/fd - 0 bytes; /var/run - 13.77 GiB; /platform/sun4v/lib/sparcv9/libc_psr.so.1 - 27.24 GiB; /var - 26.84 GiB; /proc - 0 bytes; /system/contract - 0 bytes; /rpool - 18.04 GiB; /mnt - 157.67 TiB; /vol - 0 bytes; /was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1 - 27.24 GiB; /etc/svc/volatile - 13.77 GiB; /etc/mnttab - 0 bytes; /tmp - 13.82 GiB;</p>

<h2>After:</h2>
<p class="after"></p>

Comments

0

I believe you still want your output in the same format. This should work for you:

function formatInfo(data) {
    return data.split(";").filter(function(item,i) {
        if( item.indexOf('0 bytes') >= 0 || item.indexOf('/platform/sun4v/lib/sparcv9/libc_psr.so.1') >= 0 ) {
            return null;
        }
        return item.trim();
    }).join(";").trim();
}

//Usage:

formatInfo('/devices - 0 bytes; /system/object - 0 bytes; /etc/dfs/sharetab - 0 bytes; /dev/fd - 0 bytes; /var/run - 13.77 GiB; /platform/sun4v/lib/sparcv9/libc_psr.so.1 - 27.24 GiB; / - 27.24 GiB; /var - 26.84 GiB; /proc - 0 bytes; /system/contract - 0 bytes; /rpool - 18.04 GiB; /mnt - 157.67 TiB; /vol - 0 bytes; /was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1 - 27.24 GiB; /etc/svc/volatile - 13.77 GiB; /etc/mnttab - 0 bytes; /tmp - 13.82 GiB;');

//should return:

"/var/run - 13.77 GiB; / - 27.24 GiB; /var - 26.84 GiB; /rpool - 18.04 GiB; /mnt - 157.67 TiB; /was8 - 48.26 GiB; /platform/sun4v/lib/libc_psr.so.1 - 27.24 GiB; /etc/svc/volatile - 13.77 GiB; /tmp - 13.82 GiB"

Cheers..!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.