awk search an output string between two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search an output string between two strings
# 1  
Old 12-16-2011
awk search an output string between two strings

I would like to search for strings stored in searchstringfile.txt in inputfiles.

searchstringfile.txt
HTML Code:
J./F.
Gls. Wal
F.
Tower
input1.txt
HTML Code:
What is needed is J./F. 12 var Gls. Wal 16 interp. Tower 12
input2.txt
HTML Code:
Awk shall search for F. 16 pt. J./F. 22
output.txt
HTML Code:
input1.txt J./F. = 12 var
input1.txt Gls. Wal = 16 interp. 
input1.txt Tower = 12
input2.txt F.  = 16 pt. 
input2.txt J./F. = 22
This is the most i came up with:

Code:
awk 'NR==FNR { A[$0]; }{for(N=1; N<=NF; N++) if(A[$N]) print( FILENAME,  $N " = " $(N+1)); }' searchstringfile.txt input*.txt

# 2  
Old 12-16-2011
Code:
awk 'NR==FNR{a[$0]=$0;next}{for(i=1;i<=NF;i++){t=m FS $i;m=$i;s(m,a);s(t,a)}}function s(x,b){if(x in b){print FILENAME,x,"=",$(i+1),$(i+2);i=i+2}}' searchstringfile.txt input*.txt
input1.txt J./F. = 12 var
input1.txt Gls. Wal = 16 interp.
input1.txt Tower = 12
input2.txt F. = 16 pt.
input2.txt J./F. = 22

# 3  
Old 12-17-2011
Quote:
Originally Posted by yinyuemi
Code:
awk 'NR==FNR{a[$0]=$0;next}{for(i=1;i<=NF;i++){t=m FS $i;m=$i;s(m,a);s(t,a)}}function s(x,b){if(x in b){print FILENAME,x,"=",$(i+1),$(i+2);i=i+2}}' searchstringfile.txt input*.txt
input1.txt J./F. = 12 var
input1.txt Gls. Wal = 16 interp.
input1.txt Tower = 12
input2.txt F. = 16 pt.
input2.txt J./F. = 22

The code produces always the first two instances after the searchstring and it shall not return the second instance of a searchstring.
Simply it shall only return the strings between two searchstrings.

Strings in which to be searched:
HTML Code:
input1.txt: What is needed is J./F. 12 var II Gls. Wal 16 Tower 11 interp. 
input2.txt: Awk shall search for F. 16 J./F. 22 pt.
Ouput:

HTML Code:
input1.txt J./F. = 12 var II 
input1.txt Gls. Wal = 16 
input1.txt Tower = 11 interp. 
input2.txt F. = 16 
input2.txt J./F. = 22 pt.
# 4  
Old 12-17-2011
You may try something like this:

Code:
awk 'NR == FNR { r = r ? r "|" $0 : $0; next }
{
  s = $0; c = 0; split(x, rs); split(x, rl)
  while (match(s, r) > 1) {
    rs[++c] = RSTART; rl[c] = RLENGTH
    s = substr(s, RSTART + RLENGTH)
    }
  for (i = 0; ++i <= c;) {
    printf "%s: %s = %s\n", FILENAME, substr($0, rs[i], rl[i]), \
      (rs[i + 1] - 2 > 0 ? substr($0, rs[i] + rl[i], rs[i + 1] - 2) : \
        substr($0, rs[i] + rl[i])) 
    $0 = substr($0, rs[i] + rl[i])
    }
  }' searchstringfile.txt input1.txt input2.txt

With GNU awk 4 it's easier:

Code:
awk 'NR == FNR { r = r ? r "|" $0 : $0; next }
{
  n = patsplit($0, cr, r, s)
  for (i = 0; ++i <= n;)
    printf "%s: %s = %s\n", FILENAME, cr[i], s[i] 
 }' searchstringfile.txt input1.txt input2.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Multiple search strings replaced with single string

Hi, I need someone's help in writing correct perl code. I implemented following code for "multiple search strings replaced with single string". ========================================================= #!/usr/bin/perl my $searchStr = 'register_inst\.write_t\(' |... (2 Replies)
Discussion started by: chettyravi
2 Replies

4. 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

5. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

6. 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

7. Solaris

Search string between the strings

File name : Sample.txt <ownername>Oracle< ownername> I am new to unix world , i would like to search string and return back to another sh script. bascially i want to read file Sample.txt find the string between <ownername> Sample.txt < ownername> . Gerneric way to find the string... (15 Replies)
Discussion started by: balajikalai
15 Replies

8. Shell Programming and Scripting

Using Awk to Search Two Strings on One Line

If i wanted to search for two strings that are on lines in the log, how do I do it? The following code searches for just one string that is one one line. awk '/^/ {split($2,s,",");a=$1 FS s} /failure agaf@fafa/ {b=a} END{print b}' urfile What if I wanted to search for "failure agaf@fafa"... (3 Replies)
Discussion started by: SkySmart
3 Replies

9. Shell Programming and Scripting

Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this? ---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ---------- my working file looks like this: <empty> <empty> <empty> NAME :ABC AGE :15 GENDER... (6 Replies)
Discussion started by: kingpeejay
6 Replies

10. Shell Programming and Scripting

awk search for Quoted strings (')

Hi All, I have files: 1. abc.sql 'This is a sample file for testing' This does not have quotations this also does not have quotations. and this 'has quotations'. here I need to list the hard coded strings 'This is a sample file for testing' and 'has quotations'. So i have... (13 Replies)
Discussion started by: kprattip
13 Replies
Login or Register to Ask a Question