print string before pattern match for a non standard file - linux

I have requirement were, whenever the script matches 'host2', I would like it to print the module name (text within square braces) under which the 'host2' is listed. I know grep -B would get text before the string match but my file is does not follow a standard pattern.In this case How to get text in braces which is before the string match?
[network]
host1
host2
[webserver]
host1
host9
host7
host5
host6
host2
[db]
host11
host19
Output would be :
network
webserver

You can use this awk command:
awk '/^\[.*\]/{gsub(/[][]/, ""); m=$0} $1=="host2"{print m}' file
network
webserver

How about this:
[\[a-zA-Z]+]((?=[\n\w]+host2))

awk '{gsub(/[\[\]]/,"")}$1~/network|webserver/' file
network
webserver

Related

How to highlight words from one file in another file? (Linux)

host hostname {
hardware ethernet 00:10:E0:dF:e2:Ee;
fixed-address 192.168.*.*;
ddns-hostname name;
}
I have list of ip addresses in one file. How can I highlight those addresses in dhcpd.hosts file and still to see the whole block or the whole text?
emphasized text
I've tried to use while read variable; do grep $variable dhcpd.hosts; done < iplistfile
but it's printing only one line
You probably want
grep --color=always -f iplistfile dhcpd.hosts
and you might want to add the -F and -w options too.

How to take each string (hostname) from an output?

I am trying to write a script using bash that performing show command and takes each hostname (string) from the show output and perform action (show command) on it.
For example:
root#Router2:~$ show routers
Hostname1
Hostname2
Hostname3
And i want to take each hostname (hostname 1, hostname 2 and hostname3) and perform action on each one of them.
Here is what i manage to do:
figlet Status code
u=$(tput smul)
b=$(tput bold)
n=$(tput sgr0)
echo "${b}${u}Enter server's name${n}"
read -e server
echo ""
Routershow$=(show routers)
After that, i want to take each string (hostname) from the $Routershow output. How can i do that?
Thanks
I usually use while read x command where "x" is a variable name:
show routers | while read routerName
do
echo $routerName
done
Use a loop over all items (hostnames) that result from your command (show routers). For example:
for hostname in $(show routers)
do
# access each hostname here. e.g.
echo $hostname
done
or as extension to your script:
...
Routershow=$(show routers)
for hostname in $(echo $Routershow)
do
# access each hostname here. e.g.
echo $hostname
done

How to handle regular expression in ubuntu batch script that deal with IPv6

I used radvd to generate 64-bit prefix in a format 3002:0200:00bc:098a::/64, but it appears in the eth0 of a client device in the form of 3002:200:bc:98a::/64. Now i have script that extracts this address and saves to file for eg "temp"as 3002:200:bc:98a . I need to change it now to the form 3002:0200:00bc:098a Any advice will be appreciated.
$ cat ip.txt
3002:200:bc:98a
20:1:ad:8b
$ perl -lpe 's/[^:]+/sprintf "%04s", $&/ge' ip.txt
3002:0200:00bc:098a
0020:0001:00ad:008b
[^:]+ field to transform
ge where g is for replacing all matches and e is to allow Perl code in replacement section
sprintf "%04s", $& format the matched string as required

Replace config file variables using bash script

I tried to append a value to the ssh config file but it seems that it doesn't work if specify two different values, it reads the first not the second.
It seems sensible, instead of trying to re-write the entire file, to just modify the value.
Currently the value is:
Port 22
I want to go in and change the value to:
Port 222
The best I can come up with is this:
sed -c -i "s/\(Port *= *\).*/\1$REPLACEMENT_VALUE/" /etc/ssh/sshd_config
However I know this isn't right because it's working in a way where it is expecting to have an equals sign between the value and the variable.
I need the script to do something like this:
Look for the variable (i.e Port)
Switch this line with my own line
I think you mean this,
sed 's/.*\bPort\b.*/REPLACEMENT_VALUE/' file
Example:
$ REPLACEMENT_VALUE="Port 222"
$ echo -e "foo\nPort 22" | sed "s/.*\bPort\b.*/$REPLACEMENT_VALUE/"
foo
Port 222

Search word then search next word then append

I'm trying to append hostname in configuration file using sed, because there are multiple hosts that I need to add, so I want to to automate it. I am stuck with the following situation:
Example InputFile :
#######################################################################
#
#Service: System Uptime
#######################################################################
define service{
use generic-service
host_name host1,host2,host3,host4,host5,host6,host7,host8,host9,host10,host11,host12,host13,host14,host15,host16,host17,host18,host19,host20,host21,host22,host23,host24,host25,host26,host27,host28,host29,host30
service_description System Uptime
is_volatile 0
check_period 24x7
contact_groups Test_group
notification_options c,w,r,u
check_command check_nt_uptime
max_check_attempts 3
normal_check_interval 1
retry_check_interval 1
notification_interval 120
notification_period 24x7
}
What am trying to achieve is appending new hosts to "host_name" line, but there are so many occurrences of "host_name" in the file, so to add it for a specific service I have searched for "Service: System Uptime" then in the 7th line from there I search "host_name" and append new hosts.
I have done what I want using sed:
sed '/Service: System Uptime/{N;N;N;N;N;N;N;N;/host_name/s|$|,NewHost|}' input file
Now the problem is when there are multiple hosts in there then the above sed command fails, it breaks the line and appends the host.
Any suggestions on that?
I am not sure I understand your problem and I am unable to reproduce it based on the configuration file snippet you have provided.
Having said that, I believe the following command is roughly equivalent to yours and does not depend on a "number of lines"-based seek.
sed '/Service: System Uptime/,/host_name/{/host_name/s|$|,NewHost|}' input_file
Why are you trying to use sed? This is a job for awk:
awk '/#Service: System Uptime/ {a=1}
a && /host_name/ { a=0; $0 = $0 ", new_host" } 1' input-file

Resources