Awk script searching patterns issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk script searching patterns issue
# 1  
Old 03-12-2011
Awk script searching patterns issue

Hi

I am having a file like this

Code:
 
FILE1
 
#####################
 
C16ROTINV_
REFCLK_RXL
RXBCLK32_R
REFCLK_TXL
CLK8_TXLIN
RXBCLK32_R
DCLK_TXLIN
CLK32D_TXL
RXACLK32_R
 
#####################

Code:
 
FILE 2
 
#########################
 
 input  SI_TESTONLY_R_00;
   input  CE0_TCLK_JTGCLOCKDR_JTAG;
   input  CE0_SE_INPUT_JTGCLOCKDR_JTAG;
   input  SI_JTGCLOCKDR_JTAG_R_00;
   input  CE0_TCLK_REFCLK_RCLKSEL;
   input  CE0_SE_REFCLK_RCLKSEL;
   input  CE0_SE_INPUT_REFCLK_RCLKSEL;
   input  CE0_SEFCG_REFCLK_RCLKSEL;
   input  SI_REFCLK_RCLKSEL_R_00;
   input  CE0_TCLK_REFCLK_PLLCNTL;
   input  CE0_SE_REFCLK_PLLCNTL;
   input  CE0_SE_INPUT_REFCLK_PLLCNTL;
   input  CE0_SEFCG_REFCLK_PLLCNTL;
   input  SI_REFCLK_PLLCNTL_R_00;
   input  CE0_TCLK_CLK8_TXLINK;
   input  CE0_SE_CLK8_TXLINK;
   input  CE0_SE_INPUT_CLK8_TXLINK;
   input  CE0_SEFCG_CLK8_TXLINK;
   input  CE0_TCLK_CLK32D_TXLINK;
   input  CE0_SE_CLK32D_TXLINK;
   input  CE0_SE_INPUT_CLK32D_TXLINK;
   input  CE0_SEFCG_CLK32D_TXLINK;
   input  CE0_TCLK_REFCLK_TXLINK;

 
 
###############################

Need to Search column 1 in the FILE 2 and
Sort out the Lines in FILE2

For example searching for C16ROTINV_ in the FILE2
and Sort out all the patters related to C16ROTINV_ in an order
Similarly Searching CLK32D_TXL in the FILE2 and sort out all the patterns
in increasing order

I tried to use the Below Code

Code:
 
awk 'NR==FNR{A[$1]=$1;next } {for i in A) {print A[i] }1' file1 file2

SmilieBut got stuck here , Can you please help me out

Thanks
Jaita
# 2  
Old 03-12-2011
What OS and awk version are you using?
If you need to sort and if you don't have GNU awk,
another tool (Perl, for example) might be more appropriate.

Could you also post an example of the expected output?
# 3  
Old 03-12-2011
Network AWK SCRIPT problem

Code:
 
FILE 1
 
#####################
TESTONLY
C16ROTINV_
REFCLK_RXL
RXBCLK32_R
REFCLK_TXL
CLK8_TXLIN
RXBCLK32_R
DCLK_TXLIN
CLK32D_TXL
RXACLK32_R

#####################

Code:
 
FILE2 
 
############################
     input  CE0_TCLK_TESTONLY;
   input  CE0_TCLK_JTGCLOCKDR_JTAG;
   output SO_TESTONLY_R_00;
   input  CE0_SE_INPUT_JTGCLOCKDR_JTAG;
   input  SI_JTGCLOCKDR_JTAG_R_00;
   input  CE0_TCLK_REFCLK_RCLKSEL;
   input  CE0_SE_REFCLK_RCLKSEL;
   input  CE0_SE_INPUT_REFCLK_RCLKSEL;
   input  CE0_SEFCG_REFCLK_RCLKSEL;
   input  SI_REFCLK_RCLKSEL_R_00;
  input  SI_TESTONLY_R_00;
  input  CE0_SE_INPUT_TESTONLY;

####################################

Expected Output

Suppose from FILE 1 , awk is searching TESTONLY in FILE2 so , it should search all the Patterns in FILE2 and put in a definite order

Code:
 
OUTPUT 
 
##########################
 
input  SI_TESTONLY_R_00;
input  CE0_TCLK_TESTONLY;
input  CE0_SE_INPUT_TESTONLY;
output SO_TESTONLY_R_00;

############################

Like this for all other Pins -> SI PIN COMING FIRST , CE0_TCLK PIN COMING SECOND , CE0_SE PIN COMING THIRD AND SO_ PIN COMING FOURTH

Code:
 
awk 'BEGIN (i=0;(getline<"file1")>0;i++) arr[i]=$1 } {print arr[0]}'

But not getting the result like the expected one

Can you help me out

Thanks
Jaita
# 4  
Old 03-12-2011
Try this:

Code:
awk 'NR == FNR { f2[++c] = $0; next }
{
  n = x
  for (i = 0; ++i <= c;)
    if (f2[i] ~ $0) f1[++n] = f2[i]
  if (n) {
    print "##########", $0, "##########" RS
    for (i = 0; ++i <= n;)
      print f1[i]
    print RS      
    }  
  }' file2 file1

I think there is a typo in your example output as far as the order is concerned.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching multiple patterns and construct SQL

Hello, I have attached 2 files 1) Original_table_definition.txt => which has definition of 3 tables 2) describe_table_output.txt => which has again 3 tables definition gotten thorugh doing a show table or describe table way. Now difference between 3 tables are that tablea has no... (2 Replies)
Discussion started by: nv186000
2 Replies

2. Shell Programming and Scripting

Searching multiple patterns using awk

Hello, I have the following input file: qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 qh1adm 20130711151155 : tp import all... (7 Replies)
Discussion started by: kcboy
7 Replies

3. UNIX for Dummies Questions & Answers

Help searching for patterns occuring near one another in document

I'm very new to unix and linux, so I apologize if the answer to this question should be obvious.. What I would like to know is, is there a way to search a text document ( opened in less, or some other text viewer) for any two or more patterns that appear near one another in the document? In... (1 Reply)
Discussion started by: Colonel Panic
1 Replies

4. Shell Programming and Scripting

print the next line by searching with different patterns

Hi, I am having an '.xml' file with 'n' number of lines and also having another file with '.txt' format contains values which i want to search. Now I want to print the next line with the pattern which i am searching in '.xml' file. And the loop has to repeat for different patterns which... (5 Replies)
Discussion started by: tejastrikez
5 Replies

5. Shell Programming and Scripting

searching multiple patterns in perl

Hi, I have code like: Output it is comming as: Rels: WM2 Rels: WG2 Rels: 5 - pre/prods.pl Rels: 6 Rels: 7 Rels: 8 Rels: 10 Rels: Int But i want only "Rels: 5" pattern Just above "- pre/prods.pl". By... (7 Replies)
Discussion started by: Anjan1
7 Replies

6. Shell Programming and Scripting

Searching for multiple patterns in a file

Hi All, I have a file in which i have to search for a pattern from the beginning of the file and if the pattern is found , then i have to perform a reverse search from that line to the beginning of the file to get the first occurrence of another pattern. sample input file hey what are you... (8 Replies)
Discussion started by: Kesavan
8 Replies

7. Shell Programming and Scripting

Searching for multiple patterns in files

I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command. grep -f <textfile> <filetobegrepped> I'm doing this thru a script where I... (5 Replies)
Discussion started by: bornon2303
5 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

Script for searching a pattern in 5 files and deleting the patterns given

Hi All, I have written the below script that searches for the pattern in a file and delete them if present. please can some one have a look and suggest the changes in the script. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read... (4 Replies)
Discussion started by: Shazin
4 Replies

10. UNIX for Dummies Questions & Answers

searching for two or more patterns in a line

how can I search for two or more patterns in one line using grep? for example if I want to show only the lines that have patterns "abc" and "123"? and what if I want to show only the lines that have either "abc" or "123" in them? any hint apprecited (4 Replies)
Discussion started by: metalwarrior
4 Replies
Login or Register to Ask a Question