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
# 8  
Old 01-30-2014
Hello Omkar,

grep -v will remove the line completely if pattern is matched. We can take help for awk here.

lparstat -i | grep -w "Entitled Capacity" | awk 'gsub(/[a-zA-Z]/,X,$0) gsub(/[[:punct:]]/,Y,$0) {print $0}'

R. Singh
# 9  
Old 01-30-2014
great but the output of the script as as below :

$ lparstat -i | grep -w "Entitled Capacity" | awk 'gsub(/[a-zA-Z]/,X,$0) gsub(/[[:punct:]]/,Y,$0)'

i was looking to grep only the below line :

PHP Code:
Entitled Capacity                          1.00 
out of the below output :

PHP Code:
lparstat -grep -"Entitled Capacity"
Entitled Capacity                          1.00
Entitled Capacity of Pool                  
Could you guide me how can to do that...output should be:
Entitled Capacity : 1.00

script should search for pattern "Entitled Capacity" only.

please let me know how can this be achived .
# 10  
Old 01-30-2014

Following may help.

lparstat -i | grep -w "Entitled Capacity" | awk -F":" 'NR==1 {a=$1;gsub(/[[:space:]]/,X,a); {print a OFS $2}}'

for getting colon in output.

lparstat -i | grep -w "Entitled Capacity" | awk -F":" -vs1=" : " 'NR==1 {a=$1;gsub(/[[:space:]]/,X,a); {print a s1 $2}}'

R. Singh

Last edited by RavinderSingh13; 01-30-2014 at 10:55 AM.. Reason: adding one more solution
# 11  
Old 01-30-2014
Try :

$ lparstat -i | awk '/Entitled Capacity/ && NF==4{$1=$1;print}'

Tested like this
$ cat file
Entitled Capacity                          : 1.00
Entitled Capacity of Pool                  : 1900

$ awk '/Entitled Capacity/ && NF==4{$1=$1;print}' file
Entitled Capacity : 1.00

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