awk to grab data in range then search for pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to grab data in range then search for pattern
# 1  
Old 10-04-2016
awk to grab data in range then search for pattern

im using the following code to grab data, but after the data in the range im specifying has been grabbed, i want to count how many instances of a particular pattern is found?



Code:
awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }' /var/log/xferlog


i inserted the bolded because that's my attempt to search for the string "103 error in ata file" within the range of data i specified.
# 2  
Old 10-04-2016
How about (untested as no test data available)
Code:
awk '/parmlib.*RSP/,/seqfiles.*SSD/ {print; count++; if (/103 error in ata file/) cnt2++} END { print count, cnt2 }' /var/log/xferlog

# 3  
Old 10-04-2016
Hello SkySmart,

As you have not shown sample Input_file to us, so based on your tried code, could you please try following and let me know if this helps you.
Code:
awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {if($0 ~ /103 error in ata file/){count++}; } END { print count }'  Input_file

Off course above is not tested at all.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-04-2016
Quote:
Originally Posted by RavinderSingh13
Hello SkySmart,

As you have not shown sample Input_file to us, so based on your tried code, could you please try following and let me know if this helps you.
Code:
awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {if($0 ~ /103 error in ata file/){count++}; } END { print count }'  Input_file

Off course above is not tested at all.

Thanks,
R. Singh
this works. thank you

---------- Post updated at 09:12 AM ---------- Previous update was at 09:09 AM ----------

Quote:
Originally Posted by RudiC
How about (untested as no test data available)
Code:
awk '/parmlib.*RSP/,/seqfiles.*SSD/ {print; count++; if (/103 error in ata file/) cnt2++} END { print count, cnt2 }' /var/log/xferlog

when i run this, it prints out an output even when the string i specified to search for is not found.

the output it prints out is from the first search range.

i got this to work by using Rav's example.

thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

3. Shell Programming and Scripting

sed to grab first instance of a range

Hi all, I'm new to the forum and also relatively new to sed and other such wonderfully epic tools. I'm attempting to grab a section of text between two words, but it seems to match all instances of the range instead of stopping at just the first. This occurs when I use: sed -n... (7 Replies)
Discussion started by: Lazarix
7 Replies

4. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

5. Shell Programming and Scripting

How to grab a block of data in a file with repeating pattern?

I need to send email to receipient in each block of data in a file which has the sender address under TO and just send that block of data where it ends as COMPANY. I tried to work this out by getting line numbers of the string HELLO but unable to grab the next block of data to send the next... (5 Replies)
Discussion started by: loggedout
5 Replies

6. Shell Programming and Scripting

Search for a specific data in a file based on a date range

Hi, Currently I am working on a script to automate the process of converting the log file from binary into text format. To achieve this, partly I am depending on my application’s utility for this conversion and the rest I am relying on shell commands to search for directory, locate the file and... (5 Replies)
Discussion started by: svajhala
5 Replies

7. UNIX for Dummies Questions & Answers

pattern search using grep in specific range of files

Hi, I am trying to do the following: grep -l <pattern> <files to be searched for> In <files to be searched for> , all files should of some specific date like "Apr 8" not all files in current directory. I just to search within files Apr 8 files so that it won't search in entire list of... (2 Replies)
Discussion started by: apjneeraj
2 Replies

8. Shell Programming and Scripting

awk search within a range.

I have this kind of file ABC UUIIIIIIIIIIII , HJHKJKL XYZ HHJJJJJJMMM ABC BBOOIO, PPLIOJK XYZ NMJKJKK ABC MMMM ABC OPOPO XYZ LLKLKLL I need to get all data from ABC till XYZ so output should be UUIIIIIIIIIIII (6 Replies)
Discussion started by: dinjo_jo
6 Replies

9. UNIX for Dummies Questions & Answers

search and grab data from a huge file

folks, In my working directory, there a multiple large files which only contain one line in the file. The line is too long to use "grep", so any help? For example, if I want to find if these files contain a string like "93849", what command I should use? Also, there is oder_id number... (1 Reply)
Discussion started by: ting123
1 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question