reverse search in awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reverse search in awk script
# 1  
Old 10-09-2008
reverse search in awk script

Hi,

I am new to awk. Actually I want to search a pattern A, when I get that line with pattern A then for one of the field of that line again I want search on that field (say pattern B)from start of the file.
I am using awk. Is nested searching possible in awk?

Please do the needful as soon as possible.

Thanks,
# 2  
Old 10-09-2008
I am unsure what do you need the output, line number ? if that, just use grep is ok.
grep -n "$A" "$file_name" | grep "$B" | awk '{print $1}'
Or the filed is specified? if that,
awk '$0~/A/ && $i == B{print NR}' "$file_name"

Last edited by a2156z; 10-09-2008 at 08:32 AM..
a2156z
# 3  
Old 10-10-2008
Let me clear my doubt....I am searching for pattern A, once I get that pattern in file I get that entire line and extract its $i field(pattern B) in one variable(its known in advance) and again start search for pattern B from start of the file. I am using awk.

Thanks,
# 4  
Old 10-13-2008
Let me clear my doubt....I am searching for pattern A, once I get that pattern in file I take that entire line and extract its $i field(pattern B) in one variable(its not known in advance) and again start search for pattern B from start of the file. I am using awk.

Please suggest something in awk

Thanks,
# 5  
Old 10-13-2008
search patternA first, when matched, put the $2(assume it contains patternB) in the variable pat, then go through the whole file content and print out all the lines contain patternB

Code:
awk '{
if(index($0,"patternA")!=0)
	pat=$2
arr[NR]=$0
}
END{
for(i in arr)
	if(index(arr[i],pat)!=0)
		print arr[i]
}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to search output for a value and print

GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12" BADNUMBERS="7 3 12 5 66" for eachnum in `echo ${GOODNUMBERS}` do echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold' done what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS... (10 Replies)
Discussion started by: SkySmart
10 Replies

2. Shell Programming and Scripting

awk script for contains search

I need help in awk script to do contains search and my requirement is below I need to check if the value in each column is present in any other column and print it. And in some columns these value could be existing with comma as delimiter. Sample data... (6 Replies)
Discussion started by: aramacha
6 Replies

3. Shell Programming and Scripting

awk reverse string

Hello, Can anyone explain for me in this script to reverse the string? 1) the "x=x" part, how it works? $ echo welcome | awk '{ for(i=length;i!=0;i--)x=x substr($0,i,1);}END{print x}' $ emoclew2) x seems to be an array at the END, but can it automatically print the whole array in awk? Thanks... (8 Replies)
Discussion started by: yifangt
8 Replies

4. Shell Programming and Scripting

Better and efficient way to reverse search a file for first matched line number.

How to reverse search for a matched string in a file. Get line# of the first matched line. I am getting '2' into 'lineNum' variable. But it feels like I am using too many commands. Is there a better more efficiant way to do this on Unix? abc.log aaaaaaaaaaaaa bbbbbbbbbbbbb... (11 Replies)
Discussion started by: kchinnam
11 Replies

5. Shell Programming and Scripting

Cut or awk in reverse

I may be making this too hard on myself, but I'm trying to find a way that I can use a cut or awk string to always remove the last two delimited fields of a string. Say I have PackageName-U939393-8.2.3.4.s390x.rpm But the s390x could be any string w/o periods in it, x8664 for example,... (9 Replies)
Discussion started by: cbo0485
9 Replies

6. Shell Programming and Scripting

Help with sed/awk for reverse search and print

I have a file which is DFDG START DSFDS DSDS XXX END (VIO) AADD START SDSD FGFG END and I have to print the lines between START and END (VIO). In the files there are multiple places where START would be followed by END with few lines in between but I need to print only if START is... (18 Replies)
Discussion started by: pgbuddy
18 Replies

7. Shell Programming and Scripting

Reverse script usage -Awk

Is it possible to convert EXONtoBED (Output to input)? 1. BEDtoExon.sh ------------------- awk '{ n11 = split($11, t11, ",") n12 = split($12, t12, ",") for (i = 0; ++i < n11;) { s12 = $2 + t12 print $1, s12, s12 + t11, i”E_”$4 } }' $1 ... (1 Reply)
Discussion started by: quincyjones
1 Replies

8. Shell Programming and Scripting

Using a script variable in awk search patterns

Hi all, In a script like : job_date=.... ls -l 2>/dev/null | awk -v var =$job_date ' /Name\.Version\.+\.xml$/ { How can i include a script variable job_date store in "var" in the pattern "/Name\.Version\.+\.xml$/" Thanks in advance (12 Replies)
Discussion started by: abhinav192
12 Replies

9. Shell Programming and Scripting

awk, shell script reverse engineering app generator - project

Hi, this is fantastic forum for shell programming and scripting, so please let me to introduce you with my very old concept to have web form/s with radio, select, input fields and have an application generating valid, syntax error free scripting code. The same or alike questions are asked... (2 Replies)
Discussion started by: darius2
2 Replies

10. Shell Programming and Scripting

reverse search a text file from a specified line

Hello All, I have a following task that I need to accomplish through a script or program and I am looking for some help as I have exhausted my ideas. 1. given: a text file with thousands of lines 2. find: pattern A in file and get line number ( grep -n works) 3. find: the first occurence of... (14 Replies)
Discussion started by: PacificWonder
14 Replies
Login or Register to Ask a Question