-1

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.

  1. title AHS - abc Linux Servers- which is a string
  2. Hostname
  3. 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"
αғsнιη
  • 41,407

4 Answers4

0

With awk:

awk \
-v pattern='^title AHS - abc  Linux Servers' \
-v ip="$ip" \
-v hostname="$hostname" \
'
    $0 ~ pattern {r=NR+2}
    r==NR {printf "%s %s\n",ip,hostname}
    1
' file

Note:

  • Your question states to add after 4 lines, while your output example is after 2 lines. Change the value in the script accordingly.
  • This does not edit the file in place, see here.
pLumo
  • 22,565
0
awk \
    -v pattern='title AHS - abc  Linux Servers' \
    -v ip='0.0.0.0' \
    -v host='test' \
'$0 ~ "^"pattern"$" { getline; print; print ip, host; next; }1' infile

awk -- '
BEGIN{ pattern=ARGV[1]; host=ARGV[2]; ip=ARGV[3];  ARGV[1]=ARGV[2]=ARGV[3]=""; };
$0 ~ "^"pattern"$" { getline; print; print ip, host; next; }1' "$1" "$2" "$3" infile
αғsнιη
  • 41,407
0

We can do it using sed, but before we invoke sed, we need to set up a few things.

  • Since sed treats all strings as regex, so we have to escape any char in the client var before we plug it in our sed code.

  • Create a temp file containing the ip and hostname data. This is to read in this file into the sed output stream at the appropriate time. Note this method saves us from having to escape away any special (BRE) chars on the RHS of an s/// operation.

esc_client=$(
  printf '%s\n' "$client" |
  sed -e '
   s:[][\/.^$*]:\\&:g
   s/[[:blank:]][[:blank:]]*/[[:blank:]][[:blank:]]*/g
 '
)
tmpf=$(mktemp)
printf '%s %s\n' "$ip" "$hostname" > "$tmpf"

sed -e " /^$esc_client/!b n;n;r $tmpf d " file

guest_7
  • 5,728
  • 1
  • 7
  • 13
0
#!/bin/bash
j=$1
l=$2
m=$3
echo $j
o=`awk -v j="$j" '$0 ~ j{print NR}' filename`
o=$(($o+2))
sed -i ''$o'i '$l' '$m'' filename


Tested and worked fine

output

sh script.sh "title AHS - abc  Linux Servers" "hostname" "10.0.0.1"

praveen:/tmp$ cat p
#
# 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
hostname 10.0.0.1
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"

Python

#!/usr/bin/python
import re
import sys
z=[]
l=sys.argv[1]
k=open('filename','r')
q=k.readlines()
for b in q:
    z.append(b.strip())

o=z.index(l) o=o+2 c=sys.argv[2]+" "+sys.argv[3] z.insert(o,c) for g in z: print g.strip()