How can I select the IP address from the output of the `ip route` command using 'src' - ruby

If I run below command on terminal it gives IP address value
ip route get 8.8.8.8 | grep -oP '(?<=src )(\d{1,3}.){4}'
But when I run same command with '2>&1' then it returns empty string:
output = ''
IO.popen("ip route get 8.8.8.8 | grep -oP '(?<=src )(\d{1,3}.){4}' 2>&1", 'r+') do |f|
output = f.read.strip
end
puts output.inspect
Please guide me to understand above scenario.
What modifications I need to add to get IP address.

Nothing to do with the redirection. In Ruby, backslashes in strings must be escaped. Just replace \ with \\:
output = ''
IO.popen("ip route get 8.8.8.8 | grep -oP '(?<=src )(\\d{1,3}.){4}' 2>&1", 'r+') do |f|
output = f.read.strip
end
puts output.inspect

Related

Bash regex: get value in conf file preceded by string with dot

I have to get my db credentials from this configuration file:
# Database settings
Aisse.LocalHost=localhost
Aisse.LocalDataBase=mydb
Aisse.LocalPort=5432
Aisse.LocalUser=myuser
Aisse.LocalPasswd=mypwd
# My other app settings
Aisse.NumDir=../../data/Num
Aisse.NumMobil=3000
# Log settings
#Aisse.Trace_AppliTpv=blabla1.tra
#Aisse.Trace_AppliCmp=blabla2.tra
#Aisse.Trace_AppliClt=blabla3.tra
#Aisse.Trace_LocalDataBase=blabla4.tra
In particular, I want to get the value mydb from line
Aisse.LocalDataBase=mydb
So far, I have developed this
mydbname=$(echo "$my_conf_file.conf" | grep "LocalDataBase=" | sed "s/LocalDataBase=//g" )
that returns
mydb #Aisse.Trace_blabla4.tra
that would be ok if it did not return also the comment string.
Then I have also tryed
mydbname=$(echo "$my_conf_file.conf" | grep "Aisse.LocalDataBase=" | sed "s/LocalDataBase=//g" )
that retruns void string.
How can I get only the value that is preceded by the string "Aisse.LocalDataBase=" ?
Using sed
$ mydbname=$(sed -n 's/Aisse\.LocalDataBase=//p' input_file)
$ echo $mydbname
mydb
I'm afraid you're being incomplete:
You mention you want the line, containing "LocalDataBase", but you don't want the line in comment, let's start with that:
A line which contains "LocalDataBase":
grep "LocalDataBase" conf.conf.txt
A line which contains "LocalDataBase" but who does not start with a hash:
grep "LocalDataBase" conf.conf.txt | grep -v "^ *#"
??? grep -v "^ *#"
That means: don't show (-v) the lines, containing:
^ : the start of the line
* : a possible list of space characters
# : a hash character
Once you have your line, you need to work with it:
You only need the part behind the equality sign, so let's use that sign as a delimiter and show the second column:
cut -d '=' -f 2
All together:
grep "LocalDataBase" conf.conf.txt | grep -v "^ *#" | cut -d '=' -f 2
Are we there yet?
No, because it's possible that somebody has put some comment behind your entry, something like:
LocalDataBase=mydb #some information
In order to prevent that, you need to cut that comment too, which you can do in a similar way as before: this time you use the hash character as a delimiter and you show the first column:
grep "LocalDataBase" conf.conf.txt | grep -v "^ *#" | cut -d '=' -f 2 | cut -d '#' -f 1
Have fun.
You may use this sed:
mydbname=$(sed -n 's/^[^#][^=]*LocalDataBase=//p' file)
echo "$mydbname"
mydb
RegEx Details:
^: Start
[^#]: Matches any character other than #
[^=]*: Matches 0 or more of any character that is not =
LocalDataBase=: Matches text LocalDataBase=
You can use
mydbname=$(sed -n 's/^Aisse\.LocalDataBase=\(.*\)/\1/p' file)
If there can be leading whitespace you can add [[:blank:]]* after ^:
mydbname=$(sed -n 's/^[[:blank:]]*Aisse\.LocalDataBase=\(.*\)/\1/p' file)
See this online demo:
#!/bin/bash
s='# Database settings
Aisse.LocalHost=localhost
Aisse.LocalDataBase=mydb
Aisse.LocalPort=5432
Aisse.LocalUser=myuser
Aisse.LocalPasswd=mypwd
# My other app settings
Aisse.NumDir=../../data/Num
Aisse.NumMobil=3000
# Log settings
#Aisse.Trace_AppliTpv=blabla1.tra
#Aisse.Trace_AppliCmp=blabla2.tra
#Aisse.Trace_AppliClt=blabla3.tra
#Aisse.Trace_LocalDataBase=blabla4.tra'
sed -n 's/^Aisse\.LocalDataBase=\(.*\)/\1/p' <<< "$s"
Output:
mydb
Details:
-n - suppresses default line output in sed
^[[:blank:]]*Aisse\.LocalDataBase=\(.*\) - a regex that matches the start of a string (^), then zero or more whiespaces ([[:blank:]]*), then a Aisse.LocalDataBase= string, then captures the rest of the line into Group 1
\1 - replaces the whole match with the value of Group 1
p - prints the result of the successful substitution.

assign output of memcache command to a variable in shell/bash script

I have a bash script with this code
echo -e 'get mykey\r' | nc localhost 11211
when I run the script I get this output on the terminal :
VALUE mykey 0 1
0
END
But instead of printing it on the terminal I want to assign the output of the command 'get mykey\r' | nc localhost 11211 to a variable in my bash script.
Also when I use echo -e it prints VALUE mykey 0 1 and END which I don't want in my variable.
So the expected output is that the variable should contain only the value of the corresponding key i.e in this case the variable should contain the value 0 (can be anything depending on the key which is being get) only.
What I tried :
output = 'get mykey\r' | nc localhost 11211
echo $output
but this gives output: command not found error
How do I do it?
You could just do:
output=$(echo -e 'get mykey\r' | nc localhost 11211 | awk 'NR==2')
echo "$output"
but check the man page for nc to see if it has any options to control what it outputs.

chop `%` from an output produced by `dh -H` command

I am new to scripting, I am trying to write a simple script for sensu check to create an alert for Disk Space.
The below command produces an output:
#!/usr/bin/ruby
a = `df -h / | grep -v "Filesystem" | awk '{print $5}'`
puts a
Assume that the output is 35%
now I want to strip down the %, when I try using a.chop! still it is not removing the %
Could some one please help me stripping off the % from the output.
The value returned from your command has a newline at its end:
a
# => "35%\n"
To remove the % using chop you need to strip! it first:
a.strip!
a.chop!
# => "35"
Since you're going to use this in a numeric comparison just convert it to an integer in one step:
a = `df -h / | grep -v "Filesystem" | awk '{print $5}'`
puts a.to_i
# => 66

Obtain output from a bash command in Ruby

I'm trying to obtain the output of a bash command. More precisely, I need to store the number of lines that contains a string in a file:
variable_name = AAAAAAA
PATH_TO_SEARCH = .
COMMAND = "grep -R #{variable_name} #{PATH_TO_SEARCH} | wc -l"
To execute the command I tried both methods:
num_lines = %x[ #{COMMAND} ]
num_lines = `#{COMMAND}`
but the problem is: In "num_lines" I have 1) the number of lines that contain the string (OK!) and 2) output from grep like "grep: /home/file_example.txt: No such file or directory" (NO!).
I would like to store just the first output.
Looks like you may just need to suppress the error messages.
"You can use the -s or --no-messages flag to suppress errors." found from How can I have grep not print out 'No such file or directory' errors?

Ruby: execute bash command, capture output AND dump to screen at the same time

So my problem is that I need to have the output of running the command dumped to the screen and also capture it in a variable in a ruby script. I know that I can do the second part like this:
some_variable = `./some_kickbutt`
But my problem is that I need it to still print to the console as Hudson captures that output and records it for posterity's sake.
thanks in advance for any ideas...
Just tee the stdout stream to stderr like so:
ruby -e 'var = `ls | tee /dev/stderr`; puts "\nFROM RUBY\n\n"; puts var' | nl
ruby -e 'var = `ls | tee /dev/stderr`; puts "\nFROM RUBY\n\n"; puts var' 2>&1 | nl

Resources