0

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?

leo
  • 111
  • 3
  • 1
    If you have pasted that `ASCII text, with CRLF line terminators` with your favorite web search engine, e.g. google, then you would have seen: https://stackoverflow.com/questions/39301086/ascii-text-executable-with-crlf-line-terminators – Jetchisel Nov 09 '22 at 21:56
  • 2
    See: [How to convert Windows end of line in Unix end of line (CR/LF to LF)](https://stackoverflow.com/q/3891076/3776858) – Cyrus Nov 09 '22 at 21:59
  • 5
    The commands *are* identical; the *inputs* are not. – chepner Nov 09 '22 at 21:59
  • 2
    Also, see ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) (short answer: yes, very sensitive). – Gordon Davisson Nov 09 '22 at 22:45
  • It was not entirely obvious tome that was the cause, that's why I asked. Thanks. – leo Nov 09 '22 at 23:36

0 Answers0