0

I am trying to add an || regex to a bash ``ed one-liner in a perl script, if that makes any sense.

my $result = `df -H | grep -vE '^Filesystem|tmpfs|cdrom|none'  | awk '{ print \$1 "\t" \$5 " used."}'`; 

# .Private maybe the same as /dev/sdb1 so I'm trying to remove it too 
# by trying to add || (m/\.Private/) to the above

print  "$result";

So I am removing lines from the output that start with Filesystem, tmpfs, cdrom or none, at present, but I would also like to and the "or lines containing .Private" to the one-liner, if possible...

I have the below also, but want to reproduce its results with the above code...

my @result2 =Shell::df ("-H"); 
shift @result2;   # get rid of "Filesystem..."
for( @result2 ){
next if ((/^tmpfs|tmpfs|cdrom|none/) || (m/\.Private/));
my @words2 = split('\s+', $_);
print $words2[0], "\t", $words2[4], " used\.\n";
}
oliver
  • 6,204
  • 9
  • 46
  • 50
Carpenter
  • 149
  • 1
  • 2
  • 17
  • [Don't parse `df` output, it's a stupid idea.](http://stackoverflow.com/q/6350466#comment-7437792) – daxim Oct 16 '11 at 15:07
  • The guy is probably studying and practicing, there's nothing wrong with his question. I know this is 4 years old but it's worth saying. – Paulo Pedroso Feb 19 '16 at 15:39

4 Answers4

2

I'd recommend that you get rid of the "awk" part entirely. Calling awk from inside perl is silly.

Instead, rely on capturing lines using list context, and then do your processing inside perl.

my @lines = df -H;

my @results = grep ... @lines; # perl 'grep' builtin

If you insist on using the unix grep, why not just add '|.Private' to your grep exclusion pattern?

Austin Hastings
  • 617
  • 4
  • 13
0

Your regex doesn't do what you think it does. It matches strings that start with Fileystem or that contain the other words anywhere.

Try with this:

grep -vE '^(Filesystem|tmpfs|cdrom|none)|\.Private'
Mat
  • 202,337
  • 40
  • 393
  • 406
0

You just need to add the \.Private part to the current regexp:

grep -vE '^Filesystem|tmpfs|cdrom|none|\.Private'

On a side note, the pattern ^Filesystem|tmpfs|cdrom|none might not actually do what you want, as only Filesystem is matched at the beginning of the line, the other parts will be matched if they appear anywhere in the input. To match them at the beginning, change it to:

'^Filesystem|^tmpfs|^cdrom|^none'
0

Like this?

my $result = `df -H | grep -vE '(^Filesystem|tmpfs|cdrom|none)|\.Private'  | awk '{ print \$1 "\t" \$5 " used."}'`; 
Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57