Searching and printing only required pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching and printing only required pattern
# 8  
Old 01-29-2014
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 Smilie
# 9  
Old 01-29-2014
Actually if you want to print only core this is enough

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

Here is an example might help you

Code:
$ 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
Code:
$ awk '/([[:digit:]]+[[:space:]])core([[:space:]]+$|$)/{print $2":"$1}'


Last edited by Akshay Hegde; 01-29-2014 at 12:00 PM.. Reason: missed something
# 10  
Old 01-29-2014
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.
# 11  
Old 01-29-2014
Okay

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


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 '/([[:digit:]]+[[:space:]])(core|cores)([[:space:]]+$|$|,)/{gsub(/,/,x);print $2":"$1}' file
cores:8
core:1


Last edited by Akshay Hegde; 01-29-2014 at 12:13 PM..
# 12  
Old 01-29-2014
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 :

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 which i am using is :
Code:
 
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...
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