Searching and printing only required pattern

Hi all,

i am trying to count the number of logical processors from the below output:

 
# print_manifest | grep "logical processors"
        8 cores, 16 logical processors per socket
               2 logical processors (2 per socket)

i just want to have below output :
16
2
also the search pattern should be irrespective of "per socket" word and must be purely based on (<digits> logical processors ) pattern

can someone help me in getting desired result..maybe using awk or sed..

Try:

print_manifest | perl -nle '/(\d+) logical processors/&&print $1'

excellent bartus11....i was looking for the same...i dont know perl. could you please explain me the script , it will be very helpful :slight_smile:

thanks a lot :slight_smile:

also could you please let me know how we can print the last line of this output , in this case it will be 2. i can do it using tail -1 but i want to learn how to do it using perl

Hello Omkar,

Following may help also.

awk '{for(i=1;i<=NF;i++) {if($i=="logical") print $(i-1)}}' file_name

Output will be as follows.

16
2

Thanks,
R. Singh

1 Like
print_manifest | perl -nle '/(\d+) logical processors/&&($x=$1);END{print $x}'

thanks a lot RavinderSingh13 really appreciate your help in providig me the solution.

however can we tweek this for loop to search for "logical processor" instead of only "logical".

i have created below script will search for pattern /{numerical value} core/ or /{numerical value} cores/
out of the below output of print_manifest:

 
System Hardware
    Model:              ia64 hp Integrity Virtual Partition
    Main Memory:        6137 MB
    Processors:         2
        Intel(R)  Itanium(R)  Processor 9560 (2.53 GHz, 32 MB)
        8 cores, 16 logical processors per socket
        6.38 GT/s QPI, CPU version D0
               Active processor count:
               1 socket
               1 core
               2 logical processors (2 per socket)
               LCPU attribute is enabled

script and its output :

 
# print_manifest | awk '/[[:digit:]]+/ && /core|cores/ { print "core:"$1 }' | tail -1
core:1

here i am searching for pattern /<any numerical value> core/ or /<any numerical value> cores/.

Can you please check whether i can make this script better than what it is...

Try :

$ cat file
System Hardware
    Model:              ia64 hp Integrity Virtual Partition
    Main Memory:        6137 MB
    Processors:         2
        Intel(R)  Itanium(R)  Processor 9560 (2.53 GHz, 32 MB)
        8 cores, 16 logical processors per socket
        6.38 GT/s QPI, CPU version D0
               Active processor count:
               1 socket
               1 core
               2 logical processors (2 per socket)
               LCPU attribute is enabled  

$ awk '/core|cores/ && ++i>1{print "core:"$1;exit}' file
core:1
$ print_manifest | awk '/core|cores/ && ++i>1{print "core:"$1;exit}' 
1 Like

thank you very much Akshay Hegde for providing the smarter awk script.
i can see teh pattern matching is done only for core or cores part , can u let me know how can we include the numerical value and the core|cores part inside this.

reason for this is i want to very sure that only numerical valuses followed by cores or cores word is getting searched for eg : 8 cores or 1 core

the search pattern should not provide any output for patterns like " dual core " but only search for <numerical value> core or <numerical value> cores.
It will helpfull if you show me how we can incorporate this change in your script..
thanks again :slight_smile:

Actually if you want to print only core this is enough

$ awk '/core$/{print $2":"$1;exit}'

Here is an example might help you

$ awk '/([[:digit:]]+[[:space:]])core([[:space:]]+$|$)/{print $2":"$1}' <<EOF
10 cores
2 cores
y cores
a core
1 core
10 cores
10 core abc
EOF

core:1

if only one match exit

$ awk '/([[:digit:]]+[[:space:]])core([[:space:]]+$|$)/{print $2":"$1}'

Akashay as per your example my requirement out put should be 10 ..
requirement are :
script should search only for <number> core or <number> cores and provide the last line output.

as far as your example is concerned script should exclude patterns like y cores , a cores and only search remaining patterns like :
10 cores
2 cores
1 core
10 cores

as the last line output is 10 cores.fianl output output should be core:10

thank you very much for your quick reply. i am eager to learn scripting.

Okay

$ awk '/([[:digit:]]+[[:space:]])(core|cores)([[:space:]]+$|$)/{print $2":"$1}' <<EOF
10 cores
2 cores
y cores
a core
1 core 
10 cores
10 core abc
EOF

cores:10
cores:2
core:1
cores:10
$ cat file
System Hardware
    Model:              ia64 hp Integrity Virtual Partition
    Main Memory:        6137 MB
    Processors:         2
        Intel(R)  Itanium(R)  Processor 9560 (2.53 GHz, 32 MB)
        8 cores, 16 logical processors per socket
        6.38 GT/s QPI, CPU version D0
               Active processor count:
               1 socket
               1 core
               2 logical processors (2 per socket)
               LCPU attribute is enabled 
 
$ awk '/([[:digit:]]+[[:space:]])(core|cores)([[:space:]]+$|$|,)/{gsub(/,/,x);print $2":"$1}' file
cores:8
core:1

thanks a lot Akshay....could you please explain me )([[:space:]]+$|$|,)/{gsub(/,/,x) this part of script also i am taking output of the last line that is core:1 as final output by piping this commands output with tail -1...
is this possible via awk.

thanks again for making me understand use of awk..great

also i am trying to apply same logic in finding the logical processor count using the script which u have provided from the output :

 
System Hardware
    Model:              ia64 hp Integrity Virtual Partition
    Main Memory:        6137 MB
    Processors:         2
        Intel(R)  Itanium(R)  Processor 9560 (2.53 GHz, 32 MB)
        8 cores, 16 logical processors per socket
        6.38 GT/s QPI, CPU version D0
               Active processor count:
               1 socket
               1 core
               2 logical processors (2 per socket)
               LCPU attribute is enabled

script which i am using is :

 
print_manifest | awk '/([[:digit:]]+[[:space:]])(logical processors)([[:space:]]+$|$|,)/{gsub(/,/,x);print $2":"$1}'

but its not giving any output..can u let me know hwere i am going wrong...