kttan
April 19, 2018, 11:34pm
1
OS: window 7
shell : korn shell
I have 2 file , i'm need grep data according File_1 from file 2.
File_1
CAL_ENAB_N_4_
$2N12743_29
+12V
File_2
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)
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.
rdrtx1
April 20, 2018, 12:40am
2
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
kttan
April 20, 2018, 1:39am
3
rdrtx1:
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
{gsub("\"", "", $2)}
RudiC
April 20, 2018, 3:05am
4
rdrtx1's proposal works perfectly for me, except the
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.
kttan
April 20, 2018, 3:49am
5
rudic:
rdrtx1's proposal works perfectly for me, except the
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.
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:
NF>1 {gsub("\"", "", $2)}
1 Like
The following has got some more fixes, e.g. it allows the PROBES sections to be last, and would even handle two PROBES sections.
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
1 Like