How does ps aux | grep '[p]attern' exclude grep itself? - command-line

The title says it all. I've seen this idiom used alot instead of adding an additional grep -v grep in some ps pipeline. For example it could be used like this:
$ ps aux | grep '[f]irefox' | awk '{ print $8 }'
instead of
$ ps aux | grep 'firefox' | grep -v grep | awk '{ print $8 }'
It's super-convenient, but how does it work and why?

The pattern [f]irefox will not match the literal string [f]irefox. Instead it will match strings with exactly one char from the 1-character class [f], followed by irefox.

Related

Issue in executing a command in perl

I have the below code:
chdir glob "/home/test/test1/test2/perl*";
#testList = exec "cat test.in | grep build | awk '{print \$2}'";
foreach my $testList(#testList) {
chdir "/home/test/test1/$testList";
exec "cat test.out | grep -w 'PASSED'";
}
After I run the above code, it gives the output for only the below:
chdir glob "/home/test/test1/test2/perl*";
#testList = exec "cat test.in | grep build | awk '{print \$2}'";
but not the other two lines below. I only want the output for the lines:
foreach my $testList(#testList) {
chdir "$opts->{/home/test/test1$testList";
exec "cat test.out | grep -w 'PASSED'";
and not
chdir glob "/home/test/test1/test2/perl*";
#testList = exec "cat test.in | grep build | awk '{print \$2}'";
Please help.
exec and system are returning exit code.
Try:
#testList = `cat test.in | grep build | awk '{print \$2}'`;

Finding max value of a specific date awk

I have a file with several rows and with each row containing the following data-
name 20150801|1 20150802|4 20150803|6 20150804|7 20150805|7 20150806|8 20150807|11532 20150808|12399 2015089|12619 20150810|12773 20150811|14182 20150812|27856 20150813|81789 20150814|41168 20150815|28982 20150816|24500 20150817|22534 20150818|3 20150819|4 20150820|47773 20150821|33168 20150822|53541 20150823|46371 20150824|34664 20150825|32249 20150826|29181 20150827|38550 20150828|28843 20150829|3 20150830|23543 20150831|6
name2 20150801|1 20150802|4 20150803|6 20150804|7 20150805|7 20150806|8 20150807|11532 20150808|12399 2015089|12619 20150810|12773 20150811|14182 20150812|27856 20150813|81789 20150814|41168 20150815|28982 20150816|24500 20150817|22534 20150818|3 20150819|4 20150820|47773 20150821|33168 20150822|53541 20150823|46371 20150824|34664 20150825|32249 20150826|29181 20150827|38550 20150828|28843 20150829|3 20150830|23543 20150831|6
The pipe separated value indicates the value for each of the dates in the month.
Each row has the same format with same number of columns.
The first column name indicates a unique name for the row e.g. 20150818 is yyyyddmm
Given a specific date, how do I extract the name of the row that has the largest value on that day?
I think you mean this:
awk -v date=20150823 '{for(f=2;f<=NF;f++){split($f,a,"|");if(a[1]==date&&a[2]>max){max=a[2];name=$1}}}END{print name,max}' YourFile
So, you pass the date you are looking for in as a variable called date. You then iterate through all fields on the line, and split the date and value of each into an array using | as separator - a[1] has the date, a[2] has the value. If the date matches and the value is greater than any previously seen maximum, save this as the new maximum and save the first field from this line for printing at the end.
You couldn't have taken 5 seconds to give your sample input different values? Anyway, this may work when run against input that actually has different values for the dates:
$ cat tst.awk
BEGIN { FS="[|[:space:]]+" }
FNR==1 {
for (i=2;i<=NF;i+=2) {
if ( $i==tgt ) {
f = i+1
}
}
max = $f
}
$f >= max { max=$f; name=$1 }
END { print name }
$ awk -v tgt=20150801 -f tst.awk file
name2
As a quick&dirty solution, we can perform this in following Unix commands:
yourdatafile=<yourdatafile>
yourdate=<yourdate>
cat $yourdatafile | sed 's/|/_/g' | awk -F "${yourdate}_" '{print $1" "$2}' | sed 's/[0-9]*_[0-9]*//g' | awk '{print $1" "$2}' |sort -k 2n | tail -n 1
With following sample data:
$ cat $yourdatafile
Alice 20150801|44 20150802|21 20150803|7 20150804|76 20150805|71
Bob 20150801|31 20150802|5 20150803|21 20150804|133 20150805|71
and yourdate=20150803 we get:
$ cat $yourdatafile | sed 's/|/_/g' | awk -F "${yourdate}_" '{print $1" "$2}' | sed 's/[0-9]*_[0-9]*//g' | awk '{print $1" "$2}' |sort -k 2n | tail -n 1
Bob 21
and for yourdate=20150802 we get:
$ cat $yourdatafile | sed 's/|/_/g' | awk -F "${yourdate}_" '{print $2" "$1}' | sed 's/[0-9]*_[0-9]*//g' | awk '{print $2" "$1}' | sort -k 2n | tail -n 1
Alice 21
The drawback is that only one line is printed the highest value of a day was achieved by more than one name as can be seen with:
$ yourdate=20150805; cat $yourdatafile | sed 's/|/_/g' | awk -F "${yourdate}_" '{print $2" "$1}' | sed 's/[0-9]*_[0-9]*//g' | awk '{print $2" "$1}' | sort -k 2n | tail -n 1
Bob 71
I hope that helps anyway.

Escaping pipe in perl

I'm having some trouble with backtics and pipe in perl. I have following code:
my #arr_lsdev = `lsdev -C | grep inet | awk '{print \$1}'` ;
print Dumper #arr_lsdev ;
But I get following error:
sh[2]: 0403-057 Syntax error : `|' is not expected
I'm guessing it has something to with my escape commands. I have tried escaping the | but it still results in the same error.
OS: AIX
Shell: KSH
Notice that the error is on line 2. You are actually executing
my #arr_lsdev = `lsdev -C | grep inet
| awk '{print \$1}'` ;
You can reduce the number of pipes:
my #arr_lsdev = map {(split ' ')[0]} grep {/inet/} `lsdev -C`;

Perl telnet command not sending every command

I have the following program below which telnets into another device and prints serial number and Mac address.
My problem is that for some reason if I send the command once it skips the first command and sends the second, but if I copy the same command twice it will send the command.
What is the correct way to send a command multiple commands successively?
Should the buffer be flushed after every command sent ?
My Env
Eclipse Ide
Ubuntu 12.10
perl 5, version 14, subversion 2 (v5.14.2)
Snippet of my code:
$telnet = Net::Telnet->new($remoteSystem);
$| = 1;
$telnet->buffer_empty();
$telnet->buffer_empty();
$result = $telnet->input_log($errorlog);
#$_ = "#lines";
#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2');
#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2');
#mac = $telnet->cmd('ifconfig | grep eth0 | cut -d" " -f 11');
print "#TSN AND #TSN #mac";
print FH "$remoteSystem\n";
print "Telnetting into $remoteSystem .\n"; # Prints names of the tcd
close(telnet);
}
foreach (#host) {
checkStatus($_);
}
OUTPUT That skips the first command:
bash-2.02 AND bash-2.02 ifconfig | grep eth0 | cut -d" " -f 11
00:11:D9:3C:6E:02
bash-2.02 #
bash-2.02 Telnetting into debug79-109 .
OUTPUT That works but I have to send the same command twice:
export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2
AE20001901E2FD1
bash-2.02 #
bash-2.02 AND export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2
AE20001901E2FD1
bash-2.02 #
bash-2.02 ifconfig | grep eth0 | cut -d" " -f 11
00:11:D9:3C:6E:02
bash-2.02 #
bash-2.02 Telnetting into debug79-109
Specify the command prompt in your call to cmd(), e.g.#TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2', Prompt => 'bash-2.02 #');
Try opening a connection after creating a object for the module telnet
$telnet->open($host);
After which execute waitFor method:(waits until the pattern bash-2.02 # comes)
$telnet->waitFor(/^(bash-\d+.\d+ #)$/);
and then execute your commands , it would give you proper output.

Pattern extraction using SED or AWK

How do I extract 68 from v1+r0.68?
Using awk, returns everything after the last '.'
echo "v1+r0.68" | awk -F. '{print $NF}'
Using sed to get the number after the last dot:
echo 'v1+r0.68' | sed 's/.*[.]\([0-9][0-9]*\)$/\1/'
grep is good at extracting things:
kent$ echo " v1+r0.68"|grep -oE "[0-9]+$"
68
Match the digit string before the end of the line using grep:
$ echo 'v1+r0.68' | grep -Eo '[0-9]+$'
68
Or match any digits after a .
$ echo 'v1+r0.68' | grep -Po '(?<=\.)\d+'
68
Print everything after the . with awk:
echo "v1+r0.68" | awk -F. '{print $NF}'
68
Substitute everything before the . with sed:
echo "v1+r0.68" | sed 's/.*\.//'
68
type man grep
and you will see
...
-o, --only-matching
Show only the part of a matching line that matches PATTERN.
then type echo 'v1+r0.68' | grep -o '68'
if you want it any where special do:
echo 'v1+r0.68' | grep -o '68' > anyWhereSpecial.file_ending