Searching and printing only required pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching and printing only required pattern
# 1  
Old 01-29-2014
Searching and printing only required pattern

Hi all,

i am trying to count the number of logical processors from the below output:
Code:
 
# 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..
# 2  
Old 01-29-2014
Try:
Code:
print_manifest | perl -nle '/(\d+) logical processors/&&print $1'

# 3  
Old 01-29-2014
excellent bartus11....i was looking for the same...i dont know perl. could you please explain me the script , it will be very helpful Smilie

thanks a lot Smilie

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

Last edited by omkar.jadhav; 01-29-2014 at 10:59 AM..
# 4  
Old 01-29-2014
Hello Omkar,

Following may help also.

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


Output will be as follows.

Code:
16
2


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-29-2014
Code:
print_manifest | perl -nle '/(\d+) logical processors/&&($x=$1);END{print $x}'

# 6  
Old 01-29-2014
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:

PHP Code:
System Hardware
    Model
:              ia64 hp Integrity Virtual Partition
    Main Memory
:        6137 MB
    Processors
:         2
        Intel
(R)  Itanium(R)  Processor 9560 (2.53 GHz32 MB)
        
8 cores16 logical processors per socket
        6.38 GT
/s QPICPU version D0
               Active processor count
:
               
1 socket
               1 core
               2 logical processors 
(2 per socket)
               
LCPU attribute is enabled 
script and its output :
Code:
 
# 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...
# 7  
Old 01-29-2014
Try :

Code:
$ 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

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

This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo not printing the variables with delimiters as required

This is the file which contains only neccessary values from the output of curl command i.e TEMP_FILE Proxy Hostname server0123.domain.com Proxy IP address XXX.XXX.XX.XX port 0000 Proxy Version SGOS X.X.X.X Proxy Serial # ... (5 Replies)
Discussion started by: ramprabhum
5 Replies

2. Shell Programming and Scripting

Help required in printing in specific format

Hi All, I 'm matching two files based on the first 2 columns and then populate other fields along with subtraction of few fields. I have managed to get the output. However, is there a easier way in formatting the output as shown below instead of using additional printf for getting fixed width... (4 Replies)
Discussion started by: shash
4 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

Help required in searching of pattern.

i m searching a zone file my domain name is abcd.com my zone file looks like this. abcd.com. IN SOA ns1.abcd.com. root.abcd.com. ( abcd.com. 400 IN A 15.1.1.1 Then i am searching that abcd.com have if below ip... (14 Replies)
Discussion started by: learnbash
14 Replies

5. Shell Programming and Scripting

Searching and printing the only pattern using awk,sed or perl

Hi All, i have an output of command vmstat as below : $ vmstat System configuration: lcpu=4 mem=5376MB ent=1.00 kthr memory page faults cpu ----- ----------- ------------------------ ------------ ----------------------- r b avm fre re pi... (10 Replies)
Discussion started by: omkar.jadhav
10 Replies

6. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

7. Shell Programming and Scripting

Searching using awk - Help required

Hi... I am working on script to search some records in a file based on certain fields and each record is a ASCII fixed size. I was using awk to search based on certain condition. But the length of the record is too much that awk is giving syntax error near unexpected token `(' Request... (5 Replies)
Discussion started by: ysrikanth
5 Replies

8. UNIX for Dummies Questions & Answers

Help required on Printing of Numbers, which are missing in the range

Hi Experts, Need help on printing of numbers, which are missing in the range. Pls find the details below Input 1000000002 1000000007 1234007940 1234007946 Output 1000000003 1000000004 1000000005 1000000006 1234007941 (2 Replies)
Discussion started by: krao
2 Replies

9. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

10. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies
Login or Register to Ask a Question