To check if specific string "PIN" present in the line or not -Tcl

Viewed 35

The file contains a set of data I want to compare the "PIN" on each line if a string is present store the entire line into the new variable

PIN i_hbmc_ieee1500_sel_wir                           
DIRECTION INPUT ;                    
USE SIGNAL ;                                    
PORT                                             
LAYER K3 ;                                         
RECT 2090.163000;                             
END                                  
END i_hbmc_ieee1500_sel_wir                                
PIN i_hbmc_ieee1501_sel_wir                                              
DIRECTION INPUT ;                                         
USE SIGNAL ;                   
PORT                                     
LAYER K3 ;
RECT 2090.163000;                                                                   
END                                                     
END i_hbmc_ieee1500_sel_wir wir

The output is getting 1, The code I tried is here, and the expecting outcome as

PIN i_hbmc_ieee1500_sel_wir                                           
PIN i_hbmc_ieee1501_sel_wir                               
set filedata [open "osi_hbmp_top_briscm_1.txt" r]
set file1 [ read $filedata ]
puts [string compare "PIN" $file1]
1 Answers

string compare (lexographically) compares if two strings are the same. see the docs. So 1 is returned because the input is lexographically greater then the "PIN".

If you want to print the lines starting with "PIN", you first need to split the input into lines (separating by the end-line character \n and then match the beginning:

foreach line [split $file1 \n] {
    if {[string match "PIN*" $line]} {
        puts $line
    }
}

this will return

PIN i_hbmc_ieee1500_sel_wir
PIN i_hbmc_ieee1501_sel_wir
Related