I have a file that contains the text below on a Linux system. I need to search for the string title AHS - abc Linux Servers
using sed
or awk
and then add a new entry at 2nd line after the match.
#
# AHS - ABC Linux CBTS
#
subparent AHS_ABC_NIX AHS_abct_NIX_CIN CBTS
title AHS - abc Linux Servers <---this is what I want to search in text file
group-sorted
>---here I need to add the new entry---<
10.34.73.111 lkut # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.34.73.111 lkut0 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.10.10.10 lkut1 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.10.10.10 lkut2 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.10.10.10 lkut3 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.48.12.131 lkut4 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.48.12.57 lkut5 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
I am trying to write a script that will do this by reading arguments from the command line:
#! /bin/bash
user=whoami
#file1=/home/xymon/server/bin/ghostlist.cgi
logfile=logfile1 ###Local Log file######
client="$1" #passing whole string as an argument
hostname=$2 #providing the hostname as an argument to script
ip=$3 #providing ip
####taking backup while editing hosts file everytime#####
#cp -pr /home/xymon/server/etc/hosts.cfg "/home/xymon/server/etc/hosts.$(date +"%Y%m%d")"
echo $ip
echo $hostname
echo "hostfile is modified @ date
" >>$logfile
>---sed code here---<
Expected output. let say myscript.sh is the script I'm trying to execute.
sh -x myscript.sh "title AHS - abc Linux Servers" test 0.0.0.0
so in above command I'm providing the 3 arguments to script.
title AHS - abc Linux Servers
- which is a string- Hostname
- ip address
and I'm expecting output like:-
# AHS - ABC Linux CBTS
#
subparent AHS_ABC_NIX AHS_abct_NIX_CIN CBTS
title AHS - abc Linux Servers
group-sorted
0.0.0.0 test <--- here I want to add my arguments (hostname & ip)
10.34.73.111 lkut # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.34.73.111 lkut0 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.10.10.10 lkut1 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.10.10.10 lkut2 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.10.10.10 lkut3 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.48.12.131 lkut4 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
10.48.12.57 lkut5 # testip "TRENDS:*,netstat:netstat|netstat1|netstat2|netstat3"
client="$@"
? I would expect this to beclient=$1
. – Kusalananda Dec 21 '20 at 12:29