# Grep remote multiple hosts output to local server

**URL:** <https://community.unix.com/t/grep-remote-multiple-hosts-output-to-local-server/376316>\
**Category:** Shell Programming and Scripting\
**Created:** [September 21, 2019, 12:55pm UTC](https://community.unix.com/t/grep-remote-multiple-hosts-output-to-local-server/376316 "2019-09-21T12:55:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![charli1](https://community.unix.com/letter_avatar/charli1/32/6_449c533f586939b6e5f246632a46421f.png) [@charli1](https://community.unix.com/u/charli1)\
**Post date:** [September 21, 2019, 12:55pm UTC](https://community.unix.com/t/grep-remote-multiple-hosts-output-to-local-server/376316/1 "2019-09-21T12:55:11Z")

</div>

Hello all,  
i'm trying to create a report by greping a pattern on multiple remote hosts and creta a simple report,  
actually i did this, is ther any better way to do this.

```nohighlight
#!/bin/bash
for host in `cat RemoteHosts`
do
  ssh $host -C 'hostname 2>&1; grep ERROR /var/log/WebServer.log.2019-09-21 | wc -l' >> output
done
# joining each two lines together with single tab between
sed 'N;s/\n/ /' output > JoinedLinesReport.csv
#removing old output file
rm -f output

```

The report with the above script is the following,

```nohighlight
server1 1345
server2 2345

```

Is there any way to create it like this?

```nohighlight
HostName Count
server1 1345
server2 2345

```

EDIT: added a new sed to add the line i need.  
Thanks any way.

```nohighlight
sed -i '1s/^/HostName Count\n/' JoinedLinesReport.csv

```

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [May 9, 2020, 2:23pm UTC](https://community.unix.com/t/grep-remote-multiple-hosts-output-to-local-server/376316/2 "2020-05-09T14:23:14Z")

</div>

Automatically bumped on May 9, 2020, 2:23pm UTC

Moderator comments were removed during original forum migration.

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [October 15, 2020, 3:45am UTC](https://community.unix.com/t/grep-remote-multiple-hosts-output-to-local-server/376316/3 "2020-10-15T03:45:01Z")

</div>

Automatically bumped on October 15, 2020, 3:45am UTC



---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [October 15, 2020, 10:20am UTC](https://community.unix.com/t/grep-remote-multiple-hosts-output-to-local-server/376316/4 "2020-10-15T10:20:39Z")

</div>

It is simpler to put the header **before** the loop:

```nohighlight
echo "HostName Count" > output

```

A more efficient variant follows.

```nohighlight
{
# stdout is redirected in this code block
echo "HostName Count"
for host in `cat RemoteHosts`
do
  ssh $host -C 'hostname 2>&1; grep -c ERROR /var/log/WebServer.log.2019-09-21'
done |
# joining each two lines together with single tab between
  sed 'N;s/\n/ /'
} > JoinedLinesReport.csv

```
