Simple awk search problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple awk search problem
# 1  
Old 12-07-2011
Simple awk search problem

Hello;

we have :

Code:
awk '/reg_exp/,0/

prints every line after the first occurrence of "reg_exp"

But if I want to print rest of the lines AFTER the last occurrence of "reg_exp",
how would I do it ??

Tried :

Code:
awk ' ! (/reg_exp/,0)'

But it errored...

Thank you for any suggestions..
# 2  
Old 12-07-2011
Well this
Code:
awk '/reg_exp/,0

has some syntax errors and wouldn't run.

How about a naive approach:
Code:
 awk '/regex/{flag=1;next}flag' input

EDIT: oops, I misread the part "after the last occurrence". Sorry

To find out that the occurrence is the last, awk has to search through the whole file. So you either need to scan through file twice (once in the BEGIN block, to find which is the last), or save the lines in an array, which may overflow your memory, if the file is really large. So I would make use of 'tac' which will print the lines in reverse order:

Code:
 tac lines | sed -n '/six/{q};p' | tac


Last edited by mirni; 12-07-2011 at 11:08 PM..
# 3  
Old 12-07-2011
Try...
Code:
awk '/reg_exp/{x=NR}x{y[NR]=$0}END{if(x)for(z=x;z<=NR;z++)print y[z]}' file1

# 4  
Old 12-08-2011
Code:
awk '/reg_exp/{x=NR}x{y[NR]=$0}END{if(x)for(z=x;z<=NR;z++)print y[z]}' file1

Thank you very much for this solution ..

Not sure if I understand the logic in this code..

When you have a chance, may I get you to explain it ??

Thnx again
# 5  
Old 12-09-2011
Scan though every line in the file using awk...
Code:
/reg_exp/{x=NR}
For any line that matches reg_exp, store the row number in variable "x". Note: at the end of scanning the file, variable "x" will hold the line number of the last occurance of reg_exp.
Code:
x{y[NR]=$0}
If variable "x" is not null, then store each line in array "y" using line number as the index.
Code:
END{if(x)for(z=x;z<=NR;z++)print y[z]}
At the end of the file, if reg_exp was found, print each line from array "y" starting from "x".
This User Gave Thanks to Ygor For This Post:
# 6  
Old 12-09-2011
Thank you, Ygor ..

Trying to understand more complex awk syntax ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

2. Shell Programming and Scripting

simple awk sort problem

Hello folks I have the following output UNIX95=1 ps -ef -o pcpu,user,pid,args |more %CPU USER PID COMMAND 0.03 root 0 swapper 0.08 root 1 init 0.00 root 13 net_str_cached 0.00 root 12 usbhubd 0.00 root 11 escsid 0.00 root 10... (3 Replies)
Discussion started by: delphys
3 Replies

3. Shell Programming and Scripting

another simple awk problem

Hello; I need to print two previous lines after searching for a reg exp: awk '/haywood/' should produce the following =================== p9J46THe020804 89922 Tue Oct 18 21:06 MAILER-DAEMON (host map: lookup (haywood.com): deferred) ... (1 Reply)
Discussion started by: delphys
1 Replies

4. Shell Programming and Scripting

Simple awk problem II

Hello; Trying to figure out how to keep just the contents between the two search lines: awk '/regexp_1/ ,/regexp_2/' I do not want lines containing regexp_1 and regexp_2 in the output. Thank you for any ideas Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: delphys
5 Replies

5. Shell Programming and Scripting

simple awk problem

Hello; I have the following log file: 10/11/11 10:42:02 LOCK Q Userid:284 Username=root UserPID:23158 Device:marlin batch 10/11/11 10:42:02 TableNr:226 TableName:iatkn RecId:116290398 Flags:X Q H 10/11/11 10:42:02 LOCK CONTENTION X 10/11/11 10:42:02 ... (3 Replies)
Discussion started by: delphys
3 Replies

6. Shell Programming and Scripting

How to do a simple awk search?

Hello I am trying to do global search on access log files for a date and for either 'Error|error' string ls -lrt *access* | grep "Sep 23" | awk '{print $9}'|xargs awk '/23\/Sep\/2011/ && /Error/ || /error' Above matches All lines with 'error' as well unfortunately. Is there a... (6 Replies)
Discussion started by: delphys
6 Replies

7. Shell Programming and Scripting

Search with awk problem

Hi There is one problem which i am not able to find the solution :( Suppose there are two files tmpfile1 and tmpfile2 . tmpfile1 contains data as :bash> cat tempfile1 1222 1234 1234 1234 :bash> now my code is written as getcommand="cat tmpfile2" while(getcommand | getline)... (7 Replies)
Discussion started by: aishsimplesweet
7 Replies

8. Shell Programming and Scripting

simple awk problem

pcn linus> ntpq -p remote refid st t when poll reach delay offset disp ============================================================================== +smpnn01 ntpsrv1 2 u 829 1024 377 1.46 0.793 0.85 *smpnn02 ntpsrv1 2 u ... (2 Replies)
Discussion started by: arch12
2 Replies

9. Shell Programming and Scripting

awk gsub simple problem

Hi New to shell script and awk and need assistance on this problem. I need to use a variable to substitute a string in an external file and write the changed info to another file. At first I did not know if you could use a variable as the sub value but the following showed me that I can. ... (3 Replies)
Discussion started by: hukcjv
3 Replies

10. Shell Programming and Scripting

Simple AWK script problem.

Hi all, I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below: 1 4.43 1 3.65 1 2.45 2 7.65 2 8.23 2 5.65 3 4.65 3 6.21 .. .. 120... (4 Replies)
Discussion started by: omnomtac
4 Replies
Login or Register to Ask a Question