I have two text files file1.txt and file2.txt. Both have a simple structure which is like:
site_j-hostname1
<output of ifconfig>
site_j-hostname2
<output of ifconfig>
.
.
.
site_j-hostnamen
<output of ifconfig>
Two sites so j in {1,2}. From such text files, I want to produce a series of rows each with this structure (there's only one MAC on these outputs):
<hostname>,<mac address>
On CetOS 8, I came up with:
for host in $(grep site_j-hostname filej.txt); do row="$host,"; row+=$(grep -A3 -w $host filej.txt | grep -m1 ether | xargs | cut -d ' ' -f2); echo $row; done
On one file, this returned the expected output. The other returned something like (different MACs, x is just a placeholder to not put the actual MACs):
,xx:xx:xx:xx:xx:xx
,xx:xx:xx:xx:xx:xx
,xx:xx:xx:xx:xx:xx
,xx:xx:xx:xx:xx:xx
,xx:xx:xx:xx:xx:xx
,xx:xx:xx:xx:xx:xx
,xx:xx:xx:xx:xx:xx
,xx:xx:xx:xx:xx:xx
The only difference between these is that one file was generated in a Unix system, put into a .tgz file and sent to me. The other was downloaded to Windows, put into a .rar file and sent to me.
Running file
on them gives
ASCII text, with CRLF line terminators
on one and
ASCII text
on the other.
Is it possible to fix the above so that it works all the time?