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

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching and printing the only pattern using awk,sed or perl
# 1  
Old 01-30-2014
Searching and printing the only pattern using awk,sed or perl

Hi All,

i have an output of command

as below :
PHP Code:
System configuration
lcpu=4 mem=5376MB ent=1.00
kthr    memory              page              faults              cpu
----- ----------- ------------------------ ------------ -----------------------
r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa    pc    ec
 2  1 759131 564607   0   0   0 237  455   0  39 7660 351  1  1 97  0  0.05   4.6 
out of this i am trying to match only for "lcpu=4" for eg : lcpu=<digits> .
can someone provide me some clue of how to do it either suing awk,sed or perl.

the numerical value can be more than single digits for eg lcpu =40
# 2  
Old 01-30-2014

Followoing may help.

awk '/lcpu/ {print $(NF-2)}' vmstat_output

Output will be as follows. Where vmstat_output is input file.


R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-30-2014
thanks a lot ravinder..for the below command:
awk '/lcpu/ {print $(NF-2)}' vmstat_output

this command is based on the number of fileds and it may be possible that we can have n number of fileds in the output of vmstat.

so can you please help me in making the search pattern more specific,rather than just searching for 'lcpu' , can we try to search for lcpu=<numerica value> and the print should be irrespective of the number of field , that is the output of search should only be lcpu=4 and not the other fileds like 'System configuration:mem=5376MB ent=1.00'

please suggest.
# 4  
Old 01-30-2014
Hello Omkar,

kindly let me know if following helps you.

awk -vs1="lcpu= " -F"=" 'NR==1 {a=$2} gsub(/[a-z]/,X,a) {print s1 a}' vmstat_output

R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-30-2014
This script return nothing ..Smilie...
# 6  
Old 01-30-2014

If you are not saving the output for vmstat into a file then following will help.

vmstat | awk -vs1="lcpu= " -F"=" 'NR==2 {a=$2} gsub(/[a-z]/,X,a) {print s1 a}'

R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 01-30-2014
great its really helpful and i am trying to learn awk from you..

could you please explain me the workjing of this command in short..i will try and test it with some more example..

also i am trying to get only the output which is infront of "Entitled Capacity" and exclude other lines.

i am using below grep option to get this result but i want to learn how to make it possible using awk:

command i am using :
$ lparstat -i | grep -w "Entitled Capacity" | grep -v "Entitled Capacity of Pool"
Entitled Capacity                          : 1.00

the original command is :
lparstat -i | grep -w "Entitled Capacity"
Entitled Capacity                          : 1.00
Entitled Capacity of Pool                  : 1900

from here i just want to read 'Entitled Capacity' and its value and exlcue other lines ( like :Entitled Capacity of Pool )

thanks a lot

Last edited by Franklin52; 01-30-2014 at 07:15 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

3. Shell Programming and Scripting

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... (11 Replies)
Discussion started by: omkar.jadhav
11 Replies

4. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

5. Shell Programming and Scripting

Need an awk / sed / or perl one-liner to remove last 4 characters with non-unique pattern.

Hi, I'm writing a ksh script and trying to use an awk / sed / or perl one-liner to remove the last 4 characters of a line in a file if it begins with a period. Here is the contents of the file... the column in which I want to remove the last 4 characters is the last column. ($6 in awk). I've... (10 Replies)
Discussion started by: right_coaster
10 Replies

6. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

7. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies

8. Shell Programming and Scripting

Sed - Pattern Searching

Hi, Please take a look at the below eg. I would like to search for abc() pattern first and then search for (xyz) in the next line. If I can find the pattern, then I should delete the 3 lines. I can only find the pattern and delete but I am unable to find two patterns and delete. Any... (8 Replies)
Discussion started by: sreedevi
8 Replies

9. UNIX for Dummies Questions & Answers

Perl searching and printing multiple target in the same line

Hello, I'm trying to create a program in perl called; To use the program: (at the command line)$ keyword filename note: the keyword is any word or regular expression and it should display the result just like when you 'cat' the file name but with the keyword in... (2 Replies)
Discussion started by: Horizon666
2 Replies

10. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies
Login or Register to Ask a Question