Reputation: 71
I have 3 files, file1
is a text file, file2
is a csv file and file3
is a html file, I need a bash script to do following:
Compare the first line of file1
with second column(config) of file2
if equal, copy the content of the row into an existing empty table in file3
If not equal, goto the second line of the second column(config) file2
then if equal, copy the content of the row into an existing empty table in file3
interface fa0/0
interface loopback 0
ip address 10.1.1.1 255.255.255.0
no shutdown
router OSPF 1
,, Network LAB - Final config - sample - table 1.1 - CSV - final config table,,,,,,,,
,,,,,,,,,,
,ID,Config,Config description,Config score,Hostname,Mode,Category,Subcategory,Interface,IP/Network Address (if applicable
,1,interface F0/0,Go to interface F0/0 configuration mode from global configuration mode,0%,R1,Global,Basic config,Int F0/0,F0/0,-
,2,ip address 10.1.1.1 255.255.255.0,Set IP address on interface F0/0,8%,R1,Int F0/0,Basic config,Int F0/0 - IP Address,F0/0,10.1.1.1 255.255.255.0
,3,no shutdown,Brings interface F0/0 UP,7%,R1,Int F0/0,Basic config,Int F0/0 - Brings UP,F0/0,-
<tr class="gwd-tr-rtto">
<td data-sheets-value="[null,2,"Config"]" class="gwd- td-gn3d">Config</td>
<td data-sheets-value="[null,2,"Config description"]" class="gwd-td-qwup">Config description</td>
<td data-sheets-value="[null,2,"Config score"]" class="gwd-td-d4iw">Config score</td>
<td data-sheets-value="[null,2,"Hostname"]" class="gwd-td-djzd">Hostname</td>
<td data-sheets-value="[null,2,"Mode"]" class="gwd-td-17wt">Mode</td>
<td data-sheets-value="[null,2,"Category"]" class="gwd-td-24v5">Category</td>
This is what I have so far, as I have no experience in coding its probably messy and I might totally got it wrong and need to change all of it, appreciate your help.
awk FS=\, OFS=\, '
BEGIN{t="Time , Network LAB"}
FNR==1 {next}
NR==FNR {a[$1];next}
{
if ($1 in a)
{$1="*"$1;t=t RS $0}
else
{s=s==""?$0:s RS $0}
}
END {print t RS s}'file2 file1
awk '{print $0 >> "file3" }
Upvotes: 1
Views: 245
Reputation: 40718
You can try:
awk 'NR==FNR {
a[$0]++
next
}
FNR>1{
if ($3 in a)
print $3
}' file1 FS=, file2
Output:
ip address 10.1.1.1 255.255.255.0
Upvotes: 1