awk search between 2 digits a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search between 2 digits a string
# 1  
Old 07-26-2011
awk search between 2 digits a string

I would like to search between two [:digits:] a string. I thought this would be easy. The [:digit:] is always at the beginning of a line.

The code:
Code:
gawk '/^[0-9]/{d=$1},/searchstring/,/^(d+1)/'

or
Code:
gawk '/^[0-9]/,/searchstring/,/^[0-9]/'

did not return the desired result.


inputfile.txt
999 some text searchstring some text
some text, some text
1000 some text, some text
some text
1001 some text searchstring some text
some text
1002 ....


outputfile.txt
999 some text searchstring some text
some text, some text
1001 some text searchstring some text
some text
# 2  
Old 07-26-2011
Code:
awk '/^[0-9]+/&&/searchstring/{print;getline;print}' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 07-26-2011
Code:
 
awk -v s="searchstring" '/^[0-9]/ && $0 ~ s {c=2;}c&&c--' input_file

This User Gave Thanks to panyam For This Post:
# 4  
Old 07-26-2011
Thanks @panyam & @franklin52

panyams code returns a slightly different result than franklin52's code on my files. panyams is for me the more correct + suitable. I am a novice on awk, though I figured out that always the preceeding line with getline or {c=2} is returned. There are sometimes more preceeding lines. How would the code be if there are multiple lines? The next number is always one larger than the one where the search string is in.

inputfile.txt
999 some text searchstring some text
some text, some text
some text, some text
some text, some text
1000 some text, some text
some text
1001 some text searchstring some text
some text
some text, some text
1002 ....
# 5  
Old 07-26-2011
Code:
awk '/^[0-9]+/{f=0}/^[0-9]+/&&/searchstring/{f=1}f' file

This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 07-26-2011
Not tested throughly.
Code:
 
awk -v s="searchstring" '/^[0-9]/ && $0 ~ s {a=$0;c=1;next} /^[0-9]/ &&  $0 !~ s {print a;a="";c=0;next}c==1{a=a"\n"$0} END{print a}'  input_File

This User Gave Thanks to panyam For This Post:
# 7  
Old 07-26-2011
Thanks @ Franklin52 & panyam

Amazing, two different approach, two different results but one desired and needed output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Search for string in column using variable: awk

I'm interested to match column pattern through awk using an external variable for data: -9 1:751343:T:A -9 0 T A 0.726 -5.408837e-03 9.576603e-03 7.967536e-01 5.722312e-01 -9 1:751756:T:C -9 0 T C 0.727 -5.360458e-03 9.579447e-03 7.966977e-01 5.757858e-01... (7 Replies)
Discussion started by: genome
7 Replies

3. Shell Programming and Scripting

Awk:String search more than one time and capture OP

Dear All During one of mine script developemnt i am stuch at one sub part. Requiremnt is as below kindly help me. IP file: 2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001X (unavailable) 2015-02-28 10:10:15 AL M UtranCell UtranCell=RTE001Y (unavailable) 2015-02-28 10:10:15 AL M... (6 Replies)
Discussion started by: jaydeep_sadaria
6 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

Urgent - Search a string inside awk

I need to search for string which is formed from the awk input file in a file given as input through -v option. I tried using the while (getline filename) and match(). But this approach is consuming lot of time as the input file has thousands of records. Please suggest any alternative. Thanks! (0 Replies)
Discussion started by: Cool
0 Replies

7. Shell Programming and Scripting

awk search an output string between two strings

I would like to search for strings stored in searchstringfile.txt in inputfiles. searchstringfile.txt J./F. Gls. Wal F. Towerinput1.txt What is needed is J./F. 12 var Gls. Wal 16 interp. Tower 12 input2.txt Awk shall search for F. 16 pt. J./F. 22 output.txt input1.txt J./F. = 12 var... (3 Replies)
Discussion started by: sdf
3 Replies

8. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

9. Shell Programming and Scripting

Search and remove digits (if exist) from end of the string

Hi Experts, Here is what I am trying to do. 1) say I have a file with below strings database1 database2 database3 data10gdb1 data10gdb2 databasewithoutdigit 2) I want to get the below output. (- if there is any digit at the end of the string, I need to remove it) (- Any... (3 Replies)
Discussion started by: shail_boy
3 Replies

10. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies
Login or Register to Ask a Question