user3409828
user3409828

Reputation: 71

file comparison between lines and rows

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:

  1. 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

  2. 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

file1: (sample)

interface fa0/0
interface loopback 0
ip address 10.1.1.1 255.255.255.0
no shutdown
router OSPF 1

file2: (sample)

,, 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,-

file3: (This is a table part of the html file)

<tr class="gwd-tr-rtto">
<td data-sheets-value="[null,2,&quot;Config&quot;]" class="gwd- td-gn3d">Config</td>
<td data-sheets-value="[null,2,&quot;Config description&quot;]" class="gwd-td-qwup">Config description</td>
<td data-sheets-value="[null,2,&quot;Config score&quot;]" class="gwd-td-d4iw">Config score</td>
<td data-sheets-value="[null,2,&quot;Hostname&quot;]" class="gwd-td-djzd">Hostname</td>
<td data-sheets-value="[null,2,&quot;Mode&quot;]" class="gwd-td-17wt">Mode</td>
<td data-sheets-value="[null,2,&quot;Category&quot;]" 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

Answers (1)

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

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

Related Questions