Need help search multiple condition from a file.

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Need help search multiple condition from a file.
# 1  
Old 04-20-2018
Need help search multiple condition from a file.

OS: window 7
shell : korn shell

I have 2 file , i'm need grep data according File_1 from file 2.


File_1
Code:
CAL_ENAB_N_4_
$2N12743_29
+12V

File_2
Code:
     NODE CAL_ENAB_N_4_
      PINS
        21548;
      PROBES
        P1465      3651,   46900 tn2700.1 LWT;
      WIRES
        21548 TO P1465 Blue    28    1.50;
 
        
    NODE "$2N12743_29"
      PINS
        203156;
      PROBES
        P391      85600,   68700 tn4768.1 LWT 75MIL;
      WIRES
        203156 TO P391 Blue    28   12.00;
        
        
    NODE +12V POWER
       9    12.00     2.00;
      10    12.00     2.00;
      PINS
        12376;
        12375;
        12373;

      PROBES
        P2651    144900,   88700;
        P2652    133500,   99900;
        P2653    151850,   83900 LWT;
        P2654    153050,   83900 LWT;
   

      WIRES
        12376 TO P2653 Red    28   13.00;
        12376 TO P2653 Red    28   13.00;
        12375 TO P2654 Red    28   13.00;

expected result print to file 3(all PXX below probes)
Code:
P1465     CAL_ENAB_N_4_
P391       $2N12743_29
P2651     +12V
P2652     +12V
P2653     +12V
P2654     +12V

ps: some time file two will have some like "$2N12743_29" , but the file_1 only have $2N12743_29.

Last edited by kttan; 04-20-2018 at 04:52 AM..
# 2  
Old 04-20-2018
Code:
awk '
NR==FNR {nodes[$1]=$1}
{gsub("\"", "", $2)}
$1 ~ /NODE/ {node=$2; if (nodes[$2]) {read_probes=1} else {read_probes=0}; next}
read_probes && NF==1 {if ($1 ~ /PROBES/) {print_probes=1} else {print_probes=0}; next }
print_probes && NF {print $1 "\t" node}
' File_1 File_2

# 3  
Old 04-20-2018
Quote:
Originally Posted by rdrtx1
Code:
awk '
NR==FNR {nodes[$1]=$1}
{gsub("\"", "", $2)}
$1 ~ /NODE/ {node=$2; if (nodes[$2]) {read_probes=1} else {read_probes=0}; next}
read_probes && NF==1 {if ($1 ~ /PROBES/) {print_probes=1} else {print_probes=0}; next }
print_probes && NF {print $1 "\t" node}
' File_1 File_2

Thank you for help , i'm try it , but no output is out.

if i'm comment this , other can have output, but if those have " at file_2 will not out
Code:
{gsub("\"", "", $2)}


Last edited by kttan; 04-20-2018 at 02:57 AM..
# 4  
Old 04-20-2018
rdrtx1's proposal works perfectly for me, except the
Code:
P2655     +12V
P2656     +12V
P2657     +12V

where do those come from? They're not in your sample input data.
And, show what happened in either case - code run with and without the gsub commented out.
# 5  
Old 04-20-2018
Quote:
Originally Posted by RudiC
rdrtx1's proposal works perfectly for me, except the
Code:
P2655     +12V
P2656     +12V
P2657     +12V

where do those come from? They're not in your sample input data.
And, show what happened in either case - code run with and without the gsub commented out.
opp , sorry , this not exits at the file_2 , i'm over copy.., coz too many of them , i remove it ,and miss out.
# 6  
Old 04-20-2018
In many awk versions (including GNU awk version 4.x) the gsub(,,$2) on a not present $2 does define $2 i.e. it sets NF to a higher value.
A workaround is to make sure that $2 exists:
Code:
NF>1 {gsub("\"", "", $2)}


Last edited by MadeInGermany; 04-20-2018 at 01:08 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 04-20-2018
The following has got some more fixes, e.g. it allows the PROBES sections to be last, and would even handle two PROBES sections.
Code:
awk '
NR==FNR {nodes[$1]; next}
NF>1 && $1=="NODE" {node=$2; gsub(/"/, "", node); read_probes=(node in nodes); print_probes=0; next} 
NF==1 && read_probes {print_probes=($1=="PROBES"); next}
NF>1 && print_probes {print $1 "\t" node}
' File_1 File_2

This User Gave Thanks to MadeInGermany For This Post:
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 of multiple numeric entries in an output file

Hello Tech Guys, I have two files named check.txt and output.txt Content of check.txt 620070527336551 40201800027285 620070551928314 40201800027285 620070534376312 40201800027285 620070536668046 02711306140261 620070248491123 02711306140261 620070553851296 02711306140261... (4 Replies)
Discussion started by: Xtreme
4 Replies

2. Shell Programming and Scripting

CSV File with Multiple Search Parameter

Dear Team Members, I have a unique problem. Below is the dataset which I have. I am writing a script which will read through the file and pull the invoice no. (Field 2 of C1 row). "C1",990001,"L1","HERO","MOTORCYCLE","ASIA-PACIFIC","BEIJING" "C2","CLUTCH","HYUNDAI",03032017... (13 Replies)
Discussion started by: chetanojha
13 Replies

3. Shell Programming and Scripting

Search multiple pattern in a file

I have a sample file with following output: HTTP/1.1 200 OK User: admin Set-Cookie: AMBARISESSIONID=y3v3648yqcno32nq478kw7ar;Path=/;HttpOnly Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: text/plain Vary: Accept-Encoding, User-Agent Content-Length: 6057 Server:... (4 Replies)
Discussion started by: saurau
4 Replies

4. UNIX for Dummies Questions & Answers

Search file and print everything except multiple search terms

I'm trying to find a way to search a range of similar words in a file. I tried using sed but can't get it right:sed 's/\(ca01\)*//'It only removes "ca01" but leaves the rest of the word. I still want the rest of the information on the lines just not these specific words listed below. Any... (3 Replies)
Discussion started by: seekryts15
3 Replies

5. UNIX for Dummies Questions & Answers

Create a file on UNIX with multiple columns on certain condition

I need to write the list of files to a new file in one column , the second column would contain the first line of that file (header record extracted through head -1 ) and the third column would contain the last record of that file (trailer record tail -1 ) . Example :- folder where the files... (8 Replies)
Discussion started by: IshuGupta
8 Replies

6. Shell Programming and Scripting

To search multiple string in file

hi , i am having a file where i need to take ignore the data from file1.txt and redirect to another file for eg: file1.txt AUS USA file2.txt AUS,123 NZ,11 USA,12 i am using the below code (4 Replies)
Discussion started by: rohit_shinez
4 Replies

7. Shell Programming and Scripting

Recursive search for string in file with Loop condition

Hi, Need some help... I want to execute sequence commands, like below test1.sh test2.sh ...etc test1.sh file will generate log file, we need to search for 'complete' string on test1.sh file, once that condition success and then it should go to test2.sh file, each .sh scripts will take... (5 Replies)
Discussion started by: rkrish123
5 Replies

8. Shell Programming and Scripting

Search multiple Strings in a File

Hi I want to search multiple strings in a file . But the search should start with "From" Keyword and end with before "Where" keyword. Please suggest me. Thanks (2 Replies)
Discussion started by: sboss
2 Replies

9. Shell Programming and Scripting

Search for multiple lines in large file

Hi, I have a requirement to search for a string in a large log file along with few lines before and after the the string. The following script was sufficient to search such an entry. STRING_TO_GREP="$1" FILE_TO_GREP="$2" NUMBER_OF_LINES_BEFORE=$3 NUMBER_OF_LINES_AFTER=$4 for i in `grep... (3 Replies)
Discussion started by: praveen123
3 Replies

10. Shell Programming and Scripting

How to search multiple strings in a file

Hi All, I want to search all the ksh scripts that has following details. 1. Search for "exit 0" 2. Search for "sqlldr" or sqlplus" 3. In the above files i want to search for all the script that has no "case" in it. Please advice. Thanks, Deep (2 Replies)
Discussion started by: deepakpv
2 Replies
Login or Register to Ask a Question