Grep command to search pattern corresponding to input from user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep command to search pattern corresponding to input from user
# 1  
Old 04-03-2017
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 :
Code:
"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]

Last edited by RudiC; 04-03-2017 at 05:39 AM..
# 2  
Old 04-03-2017
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.
Code:
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
# 3  
Old 04-03-2017
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.
Code:
awk '{x=$0; sub(/".*"/,"",x)} (index(x,"[")) {sn=$0} (index(x,"]") && index($0,search)) {print sn}' search='scan_out[5]' file

# 4  
Old 04-03-2017
this command output is -
Code:
    "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
Code:
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?
# 5  
Old 04-03-2017
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?

Last edited by RudiC; 04-03-2017 at 06:06 AM..
# 6  
Old 04-03-2017
As you see in below post My input file -
Code:
"COS_12_TM_4" [ ......
                                   ...... 
                                 "scan_out[5]" ] ;

What I am trying to do is that ,user will give input to the script i.e.
Code:
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
Code:
COS_12_TM_4

.
Code:
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?
# 7  
Old 04-03-2017
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.
Code:
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.
Code:
 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

Last edited by RavinderSingh13; 04-03-2017 at 06:34 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

3. UNIX for Beginners Questions & Answers

Reducing input file size after pattern search

I have a very large file with millions of entries identified by @M. I am using the following script to "extract" entries based on specific strings/patterns: #!/bin/bash if ] then file=$1 else echo "Input_file passed as an argument $1 is NOT found." exit; fi MID=(NULL "string-1"... (10 Replies)
Discussion started by: Xterra
10 Replies

4. Shell Programming and Scripting

Search file pattern using grep command

I 'm writing a script to search particular strings from log files. The log file contains lines start with *. The file may contain many other lines start with *. I need to search a particular line from my log file. The grep command is working in command line , but when i run my script, Its printing... (7 Replies)
Discussion started by: vinus
7 Replies

5. UNIX for Advanced & Expert Users

Pattern Search with grep

Hello, I have a bunch of zip files like SS_SAMPLE_101_123.zip SS_101_123.zip SS_SAMPLE_121_345.zip SS_SAMPLE_222_678.zip SS_123_890.zip SS_.zip The 'ls' should search and list the files such as SS_101_123.zip and SS_123_890.zip alone. Could you please guide me with this.... (5 Replies)
Discussion started by: tinufarid
5 Replies

6. Shell Programming and Scripting

Grep command is not search the complete pattern

I am facing a problem while using the grep command in shell script. Actually I have one file (PCF_STARHUB_20130625_1) which contain below records. SH_5.55916.00.00.100029_20130601_0001_NUC.csv.gz|438|3556691115 SH_5.55916.00.00.100029_20130601_0001_Summary.csv.gz|275|3919504621 ... (2 Replies)
Discussion started by: sumit.vedi1988
2 Replies

7. Shell Programming and Scripting

Pattern search using grep command !

Hi, I am trying to do pattern search using grep command. But i donot know what mistake i'm doing. I am not getting the expected Result. could any one please help me out? $ cat b.ksh AasdjfhB 57834B 86234B 472346B I want to print the line which is starting with either A or 8 and... (10 Replies)
Discussion started by: nikesh29
10 Replies

8. Shell Programming and Scripting

search for a pattern using grep

Hi I am facing the below problem. I have set of lines in which i have to search for only the line which matches with the pattren "/" only. input:- /*+ some text */ /*+ some text */ /* Remove rows from a table of survey results. */ /* Add a survey respondent's name and answers. */ /*... (7 Replies)
Discussion started by: manasa_vs
7 Replies

9. Shell Programming and Scripting

How to assign the Pattern Search string as Input Variable

guys, I need to know how to assing pattern matched string as an input command variable. Here it goes' My script is something like this. ./routing.sh <Server> <enable|disable> ## This Script takes an input <Server> variable from this line of the script ## echo $1 | egrep... (1 Reply)
Discussion started by: raghunsi
1 Replies

10. Shell Programming and Scripting

search pattern by grep

hai folks, i am vijay very new to this website. My query: Search patterns from root directory to all directories by using grep (3 Replies)
Discussion started by: vijaysabari
3 Replies
Login or Register to Ask a Question