LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-09-2019, 11:22 AM   #1
gaetan321
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Rep: Reputation: Disabled
Echo output on separate lines


Hi all.

I have to script the following to automate a report

Code:
#!/bin/sh

LOG=notification_check

RESULT=$(grep -B10 'notifications_enabled' /usr/local/nagios/etc/services.cfg |grep 'host_name\|service_description')
echo -e $RESULT "\n\v" > $LOG
cat $LOG
but the output is using a single line instead of separate lines

Code:
grep -B10 'notifications_enabled' /usr/local/nagios/etc/services.cfg |grep 'host_name\|service_description'
How can I output and echo the result with the same tabulation ?

Thanks
Cheers.
G
 
Old 04-09-2019, 11:28 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
show output, and what you want as output
 
Old 04-09-2019, 11:56 AM   #3
gaetan321
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
show output, and what you want as output

grep -B10 'notifications_enabled' /usr/local/nagios/etc/services.cfg |grep 'host_name\|service_description' outputs

host_name Server1
service_description check-WIN-Uptime
host_name Server1
service_description check_WIN_CPU


The script outputs the result into a single line

host_name Server1 service_description check-WIN-Uptime host_name Server1 service_description check_WIN_CPU

How can I output into the same original format ?
 
Old 04-09-2019, 03:00 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
the pattern looks like it gathers one match data, match data, So I put it in an array and printed it out as such. two pieces of information at a time.

Code:
#!/bin/bash

LOG=notification_check

#RESULT=$(grep -B10 'notifications_enabled' /usr/local/nagios/etc/services.cfg |grep 'host_name\|service_description')

#for test purposes already populated file with output
#host_name Server1 service_description check-WIN-Uptime host_name Server1 service_description check_WIN_CPU

RESULT=$(cat ~/output)
#echo $RESULT

#to blank out log file on each run. can be removed, of course. 
echo "" > $LOG

#put into an array
results=( $RESULT )
for (( i=0;i<${#results[@]};i++))
{
    echo "${results[i]} ${results[i+1]}" >> $LOG

    ((i++)) #add  one  to match array element positions

}
echo "START"
cat $LOG
my results
Code:
 
 $ ./printoutthis
START

host_name Server1
service_description check-WIN-Uptime
host_name Server1
service_description check_WIN_CPU
if someone else knows how to do this in awk or whatever else, more power to them. pls share.

Last edited by BW-userx; 04-09-2019 at 03:13 PM.
 
1 members found this post helpful.
Old 04-09-2019, 03:43 PM   #5
gaetan321
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
THANK YOU

BW-userx, thank you so much. It does the job
 
Old 04-09-2019, 03:54 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
That's what I'm talkin' about! enjoy. !
 
Old 04-09-2019, 04:27 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
You don't need an array here.
You simply must quote $variables in command arguments!
Code:
echo "$RESULT"
echo is a command. The quotes prevent the shell from word splitting (your problem) and trying a glob match and eventual substitution.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echo 0:$(echo 8*35*37*47 | bc) | xxd -r && echo $(id -un) Linuxanity LinuxQuestions.org Member Intro 1 08-15-2012 06:30 PM
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 07:02 PM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 01:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration