Grep command to search pattern corresponding to input from user

One more question:
I want to grep "COS_12_TM_4 pattern from a file look likes :

"COS_12_TM_4" [ 
"dummy_COS_12_1_TM_4",
"dummy_COS_12_2_TM_4",
"dummy_COS_12_3_TM_4",
"dummy_COS_12_4_TM_4",
"dummy_COS_12_5_TM_4",
"dummy_COS_12_6_TM_4",
"dummy_COS_12_7_TM_4",
"dummy_COS_12_8_TM_4",
"dummy_COS_12_9_TM_4",
"dummy_COS_12_10_TM_4",
"dummy_COS_12_11_TM_4",
"dummy_COS_12_12_TM_4",
"dummy_COS_12_13_TM_4",
"dummy_COS_12_14_TM_4",
"dummy_COS_12_15_TM_4",
"dummy_COS_12_16_TM_4",
"dummy_COS_12_17_TM_4",
"dummy_COS_12_18_TM_4",
"dummy_COS_12_19_TM_4",
"dummy_COS_12_20_TM_4",
"dummy_COS_12_21_TM_4",
"dummy_COS_12_22_TM_4",
"dummy_COS_12_23_TM_4",
"dummy_COS_12_24_TM_4",
"dummy_COS_12_25_TM_4",
"dummy_COS_12_26_TM_4",
"dummy_COS_12_27_TM_4",
"dummy_COS_12_28_TM_4",
"dummy_COS_12_29_TM_4",
"dummy_COS_12_30_TM_4",
"dummy_COS_12_31_TM_4",
"dummy_COS_12_32_TM_4",
"dummy_COS_12_33_TM_4",
"dummy_COS_12_34_TM_4",
"dummy_COS_12_35_TM_4",
"dummy_COS_12_36_TM_4",
"scan_out[5]" ];

I am taking scan_out[5] as the input from the user.
How to search "COS_12_TM_4" in the file which is corresponds to scan_out[5]

Hello Preeti Chandra,

Kindly use CODE TAGS for your Input_file/commands/codes which you are using into your posts(see example how I am using), if you want to only search for a string then following may help you in same too.

grep  "COS_12_TM_4"  Input_file
 

It will only look for above string, if you have more conditions then kindly post them with sample Input_file and expected sample output in CODE TAGS please.

Thanks,
R. Singh

I think the o/p wants to search for "scan_out[5]" and get the matching "COS_12_TM_4" .
The following searches for [ outside the "string" and on success stores the line, and it searches for ] outside the "string" and on success tries to find the given string and on success prints the stored line.

awk '{x=$0; sub(/".*"/,"",x)} (index(x,"[")) {sn=$0} (index(x,"]") && index($0,search)) {print sn}' search='scan_out[5]' file

this command output is -

    "scan_out[5]" : output;      ## pinName = scan_out[5];  tf =  SO  ; 
    "COS_12_TM_4" [ 
    "scan_out[5]" := output [ 0ns:X, 50.000000ns:Q, 100.000000ns:X ]; 

I have other 3 matches of scan_out in my input file.
Can you explain the command

awk '{x=$0; sub(/".*"/,"",x)} (index(x,"[")) {sn=$0} (index(x,"]") && index($0,search)) {print sn}' search='scan_out[5]' file

.

what exactly it is doing?

That specification is by no means clear. Please elaborate

  • do you want to match "COS_12_TM_4 or "COS_12_TM_4" ?
  • do you want to match that single line only, or the entire record?
  • how would a record be defined?
  • how does scan_out[5] come into play, with or without double quotes?

Does your input data sample represent the real data?

As you see in below post My input file -

"COS_12_TM_4" [ ......
                                   ...... 
                                 "scan_out[5]" ] ;

What I am trying to do is that ,user will give input to the script i.e.

scan_out[5]

I want to search the scan_out[5] in my input file and print the corresponding value,in this case value is

COS_12_TM_4

.

awk '{x=$0; sub(/".*"/,"",x)} (index(x,"[")) {sn=$0} (index(x,"]") && index($0,search)) {print sn}' search='scan_out[5]' file

This command helping me in grepping the corresponding value but there are other places in input file where scan_out[5] is present.So how to grep only required value?

Hello Preeti Chandra,

As you haven't shown complete sample Input_file so it is not tested completely, could you please try following and let me know if this helps you.

awk '/ \[/{gsub(/\"/,"",$1);VAL=$1;} /scan_out\[5\]/{print VAL}'  Input_file

EDIT: With MadeInGermany's approach you could do following changes in that code and get it as follows.

 awk '{x=$0; sub(/".*"/,"",x)} (index(x,"[")) {gsub(/\"/,"",$1);sn=$1} (index(x,"]") && index($0,search)) {print sn}' search='scan_out[5]'   Input_file
 

Thanks,
R. Singh

Hi,

My input file size is huge,but whatever i pasted here ,you can consider as input.

Output of above command is-


COS_12_TM_4
scan_out[5]
scan_out[9]

I want only - COS_12_TM_4 term

Hello Preeti Chandra,

Let's say following is Input_file.

cat Input_file
"COS_12_TM_2" [ ......
                                   ......
                                 "scan_out[51]" ] ;
"COS_12_TM_24" [ ......
                                   ......
                                 "scan_out[2]" ] ;
"COS_12_TM_4" [ ......
                                   ......
                                 "scan_out[5]" ] ;
"COS_12_TM_14" [ ......
                                   ......
                                 "scan_out[3]" ] ;

Then my code has give me the expected output mentioned by you as follows only.

awk '/ \[/{gsub(/\"/,"",$1);VAL=$1;} /scan_out\[5\]/{print VAL}' Input_file
COS_12_TM_4

Let me know if you have any queries or your Input_file looks different.

Thanks,
R. Singh

Yes my input file is different .I try to point the area where scan_out[5] is present in my input file :

pattern MAIN ( "dft_jtag_sel", "dft_jtag_si", "dtm_global_rstb",  
               "DTM_VTRIM":I, "VDDA_1V8_DTM":I, "VDD_0V9":I, "VSS":I, 
               "scan_out[0]", "scan_out[10]", "scan_out[11]", "scan_out[12]", "scan_out[13]", "scan_out[14]", "scan_out[15]", "scan_out[1]", "scan_out[2]", "scan_out[3]", "scan_out[4]", "scan_out[5]", "scan_out[6]", "scan_out[7]", "scan_out[8]", "scan_out[9]", 
               "DTM_VTRIM":O, "VDDA_1V8_DTM":O, "VDD_0V9":O, "VSS":O ) 
"scan_out[15]" : output;      ## pinName = scan_out[15];  tf =  SO  ;  
    "scan_out[4]" : output;      ## pinName = scan_out[4];  tf =  SO  ; 
    "scan_out[5]" : output;      ## pinName = scan_out[5];  tf =  SO  ; 
    "scan_out[6]" : output;      ## pinName = scan_out[6];  tf =  SO  ; 
 "scan_out[1]" := output [ 0ns:X, 50.000000ns:Q, 100.000000ns:X ]; 
    "scan_out[2]" := output [ 0ns:X, 50.000000ns:Q, 100.000000ns:X ]; 
    "scan_out[3]" := output [ 0ns:X, 50.000000ns:Q, 100.000000ns:X ]; 
    "scan_out[4]" := output [ 0ns:X, 50.000000ns:Q, 100.000000ns:X ]; 
    "scan_out[5]" := output [ 0ns:X, 50.000000ns:Q, 100.000000ns:X ]; 
    "scan_out[6]" := output [ 0ns:X, 50.000000ns:Q, 100.000000ns:X ]; 
 "scan_out[3]" : output;      ## pinName = scan_out[3];  tf =  SO  ; 
    "scan_out[4]" : output;      ## pinName = scan_out[4];  tf =  SO  ; 
    "scan_out[5]" : output;      ## pinName = scan_out[5];  tf =  SO  ; 
    "scan_out[6]" : output;      ## pinName = scan_out[6];  tf =  SO  ; 
    "scan_out[7]" : output;      ## pinName = scan_out[7];  tf =  SO  ; 
Last same which i posted earlier as input file.
COS_12_TM_4" [ 
         "dummy_COS_12_1_TM_4",
         "dummy_COS_12_2_TM_4",
         "dummy_COS_12_3_TM_4",
         "dummy_COS_12_4_TM_4",
         "dummy_COS_12_5_TM_4",
         "dummy_COS_12_6_TM_4",
         "dummy_COS_12_7_TM_4",
         "dummy_COS_12_8_TM_4",
         "dummy_COS_12_9_TM_4",
         "dummy_COS_12_10_TM_4",
         "dummy_COS_12_11_TM_4",
         "dummy_COS_12_12_TM_4",
         "dummy_COS_12_13_TM_4",
         "dummy_COS_12_14_TM_4",
         "dummy_COS_12_15_TM_4",
         "dummy_COS_12_16_TM_4",
         "dummy_COS_12_17_TM_4",
         "dummy_COS_12_18_TM_4",
         "dummy_COS_12_19_TM_4",
         "dummy_COS_12_20_TM_4",
         "dummy_COS_12_21_TM_4",
         "dummy_COS_12_22_TM_4",
         "dummy_COS_12_23_TM_4",
         "dummy_COS_12_24_TM_4",
         "dummy_COS_12_25_TM_4",
         "dummy_COS_12_26_TM_4",
         "dummy_COS_12_27_TM_4",
         "dummy_COS_12_28_TM_4",
         "dummy_COS_12_29_TM_4",
         "dummy_COS_12_30_TM_4",
         "dummy_COS_12_31_TM_4",
         "dummy_COS_12_32_TM_4",
         "dummy_COS_12_33_TM_4",
         "dummy_COS_12_34_TM_4",
         "dummy_COS_12_35_TM_4",
         "dummy_COS_12_36_TM_4",
         "dummy_COS_12_37_TM_4",
         "dummy_COS_12_38_TM_4",
         "dummy_COS_12_39_TM_4",
         "dummy_COS_12_40_TM_4",
         "scan_out[5]" ]; 

Lastly I want output from the last part of the file.

---------- Post updated at 04:58 AM ---------- Previous update was at 04:46 AM ----------

Can you explain me your command?
I will try at my end.

Hello Preethi Chandra,

Your Input_file is not clear, please mention it more clearly. I have considered a Input_file which I posted in my previous post too, following is explanation of command.

awk '/ \[/                    #### Searching for a line which has " [" in it, so here I am using \[ to tell awk that take [ char to be searched not it's special meaning to be searched.
{gsub(/\"/,"",$1);        #### Now if that string found in a line then removing "(which they are there in the very first field) from first field.
VAL=$1;}                   #### creating a variable named VAL and storing $1's(fist field)'s value in it.
/scan_out\[5\]/           #### Now I am searching for string scan_out[5], here again I have escaped the characters [ and ] so that they should be taken as it is by awk.
{print VAL}                #### So if above string found in any line of Input_file then print the value of variable VAL which is nothing but your expected output.
' Input_file                 #### Mentioning the Input_file here too.

Also do not run above command it is only for explaining purposes, you should run my previous post command only. Let us know clearly what is your sample Input_file on single code tags then all the conditions by which you want to achieve the expected output too.

Thanks,
R. Singh

Thank you very much for the explanation...

I am not getting the following command purpose-

{gsub(/\"/,"",$1);        #### Now if that string found in a line then removing "(which they are there in the very first field) from first field.

Hello Preeti,

You could use THANKS button at left post corner for any useful post to anyone. So here is why I have used that command:
In your Input_file eg--> "COS_12_TM_2" first field is having chars " so you don't want them into your output so I had removed it, by using awk 's global substitution keyword.

Thanks,
R. Singh