2

How do I parse the df-Bk Linux command output with C# regular expressions?

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             7913216K  2348412K  5165992K  32% /
varrun                 257788K      108K   257680K   1% /var/run
varlock                257788K        0K   257788K   0% /var/lock
udev                   257788K       56K   257732K   1% /dev

I wish to get "1K-blocks" and "Used".

5
  • I think you should provide more to get help. Commented Jun 14, 2011 at 11:35
  • Example output would go a long way to helping here. Commented Jun 14, 2011 at 11:35
  • Can you show the example of command and describe what exactly you need to parse? Commented Jun 14, 2011 at 11:36
  • have you considered piping the output of df through awk first? (linuxfocus.org/English/September1999/article103.html) Commented Jun 14, 2011 at 13:22
  • How does your regex so far look like? Do you speak regex at all, or are you looking for code monkeys? Commented Jun 14, 2011 at 13:32

2 Answers 2

5

Regex is not a magic parsing bullet. Rather than try to parse with regex, why not get the output you want more directly and just read it?

df -Bk| awk '{print $2, $3}'

This will get you a set of lines with space-delimited fields. Parsing this becomes a matter of calling string.Split()

string[] output_lines; // presumably holds the output of df
for (int i = 0; i < output_lines.Length; i++)
{
    if (i == 0)
        continue;

    string[] b_u = output_lines[i].Split(" ");
    string blocks = b_u[0];
    string used = b_u[1];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could use ^\S+\s+(\S+) or split on \s+, skip first line tho.

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.