awk search within a range.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search within a range.
# 1  
Old 08-08-2010
awk search within a range.

I have this kind of file


Code:
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
Code:
UUIIIIIIIIIIII 
HJHKJKL
BBOOIO
PPLIOJK
MMMM
OPOPO

I did this way

Code:
awk /ABC/,/XYZ/ FILENAME

but it does not searches well , since in case where XYZ is not followed by ABC it needs to end so it searches from ABC to till next XYZ if XYZ is not found as in case of
ABC MMMM
it should return MMMM .
# 2  
Old 08-08-2010
Code:
nawk '/^ABC/{print $2 ; next} NF&&!/^XYZ/{print $1}' infile.txt

Code:
o/p:-
UUIIIIIIIIIIII
HJHKJKL
BBOOIO,
PPLIOJK
MMMM
OPOPO

SmilieSmilieSmilie
# 3  
Old 08-08-2010
Thanks will test it out.
# 4  
Old 08-09-2010
Code:
awk 'NF&&!/^XYZ/ {print (/^ABC/)?$2:$1}' urfile

# 5  
Old 08-09-2010
Does not works well.

Code:
nawk '/ABC/{print $2 ; next} NF&&!/XYZ/{print $1}' infile.txt

In case where ABC or XYZis not at start of the line it does not gives the desired results.

Below is the data when setlist in vi.
Code:
--^I^IABC^IDATA$
--^I^IXYZ^IOPOPOP^I

Also ! does not work

Code:
nawk '/^ABC/{print $2 ; next} NF&&!/XYZ/{print $1}' infile.txt

/XYZ/: Event not found.
# 6  
Old 08-09-2010
Try this code:-

Code:
nawk ' /^[ |\t]*ABC/{print $2 ; next} NF&&!/^[ |\t]*XYZ/{print $1}' infile.txt

Note:- if you don't have nawk use /usr/xpg4/bin/awk.

SmilieSmilieSmilie
# 7  
Old 08-11-2010
I have een able to figure out the problem now i dont know how do i do this.
I need to check if after ABC another ABC exists if yes then check as usual for XYZ, just to make sure if ABC is repeated on 2 lines all lines after ABC is not missed out.

As it would happen in.

Code:
ABC ABBCPP
ABC IIOIO
WHERE IOOPPPO

So in this case i'm not able to capture IIOIO
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

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? awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }'... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

Search for string on a range of lines

Hi, I am trying to parse a file searching for specific set of string and then within those set of strings finding a keyword. The script works the way that I intended it to be but I thought it could be a lot simpler may be. Any advice will be much appreciated. The script at the moment is as... (1 Reply)
Discussion started by: newbie_01
1 Replies

4. Shell Programming and Scripting

Streamline script to search for numbers in a certain range

Hello all, I need help making a script run faster since I have a huge file to sift through. The file I am running my script on looks like this: 1 -1.9E+001 -1.8E-001 1.5E+001 3.32E+001 2 -1.7E+001 -1.0E-002 1.2E+001 6.37E+001 3 -1.5E+001 -3.8E-006 6.7E+001 4.81E+001 The... (12 Replies)
Discussion started by: butson
12 Replies

5. Shell Programming and Scripting

Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range. I dont know how to go about this. I am hoping that the... (5 Replies)
Discussion started by: lostincashe
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

Using REGEX within SED range search

test data: Code: sed -n '/^**$*/,/;/{/;/G;p;}' What i'm trying to do with the above regex (in bleu) identify upper/lower case select only when select is at the beginning of the line OR preceded by a space select is followed by a space or is at the end of the line. ... (13 Replies)
Discussion started by: danmauer
13 Replies

9. Shell Programming and Scripting

Search files between a date range

Hi people A newbie here, thrown into the deep end. I want to select the group of files with in a range of dates and perform some operation on it. Are there inbuild date libraries i can use? I did read thru the old posts on this topic. Couldnt get much idea :(, basically want to know how I... (7 Replies)
Discussion started by: zcanji
7 Replies

10. Shell Programming and Scripting

Perl search and replace in range using variable

Hi. I have a file with asterisk field separators and backslash line terminators. The first field in each line names the line type. I am trying to process each range separately. Here's what the data looks like: BA*DATA\ LS*DATA1*DATA2*00020*\ TA*DATA1*DATA2*DATA3*\ TA*DATA1*DATA2*DATA3*\... (1 Reply)
Discussion started by: yoi2hot4ya
1 Replies
Login or Register to Ask a Question