Find and replace in shell script - shell

I'm trying to replace the IP with New Ip but i'm not able to do so
Network_settings="1.1.1.1:8.9.0.0:9.9.9.9: IP ADDRESS: SUBNET MASK: GATEWAY"
val=`echo "$Network_settings"| cut -d ":" -f1`;
sed -ri 's/(\b[0-9]{1,3}\.){3}[0-9]{1,3}/ 'echo "$val"'/g' Network_settings.txt

val=$( echo "$Network_settings" | awk -F: 'BEGIN{OFS=":"}{$1="2.2.2.2"; print $0}')
This sets the new ip to 2.2.2.2
This uses awk.

The shell is not going to execute the echo command in this line:
sed -ri 's/(\b[0-9]{1,3}\.){3}[0-9]{1,3}/ 'echo "$val"'/g' Network_settings.txt
One way to do it is to use double-quotes for the sed script:
sed -ri "s/(\b[0-9]{1,3}\.){3}[0-9]{1,3}/$val/g" Network_settings.txt

Here's an example using sed:
sed -r 's/\b([0-9]{1,3}\.){3}[0-9]{1,3}\b/'$val'/'

Related

nslookup/dig/drill commands on a file that contains websites to add ip addresses

UPDATE : Still open for solutions using nslookup without parallel, dig or drill
I need to write a script that scans a file containing web page addresses on each line, and adds to these lines the IP address corresponding to the name using nslookup command. The script looks like this at the moment :
#!/usr/bin/
while read ip
do
nslookup "$ip" |
awk '/Name:/{val=$NF;flag=1;next} /Address:/ &&
flag{print val,$NF;val=""}' |
sed -n 'p;n'
done < is8.input
The input file contains the following websites :
www.edu.ro
vega.unitbv.ro
www.wikipedia.org
The final output should look like :
www.edu.ro 193.169.21.181
vega.unitbv.ro 193.254.231.35
www.wikipedia.org 91.198.174.192
The main problem i have with the current state of the script is that it takes the names from nslookup (which is good for www.edu.ro) instead of taking the aliases when those are available. My output looks like this:
www.edu.ro 193.169.21.181
etc.unitbv.ro 193.254.231.35
dyna.wikimedia.org 91.198.174.192
I was thinking about implementing a if-else for aliases but i don't know how to do one on the current command. Also the script can be changed if anyone has a better understanding of how to format nslookup to show it like the output given.
Minimalist workaround quasi-answer. Here's a one-liner replacement for the script using GNU parallel, host (less work to parse than nslookup), and sed:
parallel "host {} 2> /dev/null |
sed -n '/ has address /{s/.* /'{}' /p;q}'" < is8.input
...or using nslookup at the cost of added GNU sed complexity.
parallel "nslookup {} 2> /dev/null |
sed -n '/^A/{s/.* /'{}' /;T;p;q;}'" < is8.input
...or using xargs:
xargs -I '{}' sh -c \
"nslookup {} 2> /dev/null |
sed -n '/^A/{s/.* /'{}' /;T;p;q;}'" < is8.input
Output of any of those:
www.edu.ro 193.169.21.181
vega.unitbv.ro 193.254.231.35
www.wikipedia.org 208.80.154.224
Replace your complete nslookup line with:
echo "$IP $(dig +short "$IP" | grep -m 1 -E '^[0-9.]{7,15}$')"
This might work for you (GNU sed and host):
sed '/\S/{s#.*#host & | sed -n "/ has address/{s///p;q}"#e}' file
For all non-empty lines: invoke the host command on the supplied host name and pipe the results to another invocation of sed which strips out text and quits after the first result.

How do I get sed to use a user input variable?

It doesn't seem like sed is aware of the $IP variable.
How can I get the following to work?
read -p "Please enter the line number with the IP you wish to block from accessing the Internet? " IP
echo $IP
IP_LINE_NUMBER=`nmap -n -sn 192.168.3.0/24 -oG - | awk '/Up$/{print $2}' | sed '$IPq;d'`
echo $IP_LINE_NUMBER
You have to use double quotes and put {} around IP:
ip_line_number=$(nmap -n -sn 192.168.3.0/24 -oG - | awk '/Up$/{print $2}' | sed "${IP}q;d")
Note: you need command substitution $(...) to get the output into your variable
See also:
Difference between single and double quotes in Bash
Correct Bash and shell script variable capitalization

using makefile variable in sed command [duplicate]

This question already has an answer here:
Sed command in makefile
(1 answer)
Closed 6 years ago.
I have tried putting the following command in makefile.
#get Local Ip Address
LOCALIP=$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}') &
#get Web Url from User
#read -p "Enter Web Url:" weburl; \
sed -e "\|$LOCALIP $weburl|h; \${x;s|$LOCALIP $weburl||;{g;t};a\\" -e "$LOCALIP $weburl" -e "}" hosts.txt
When I try to execute the command, I expected to get the sed command like following:
sed -e "\|192.168.5.1 www.weburl.com|h; \${x;s|192.168.5.1 www.weburl.com||;{g;t};a\\" -e "192.168.5.1 www.weburl.com" -e "}" hosts.txt
But, I get the following,
sed -e "\|/s/$/OCALIP eburl|h; \" hosts.txt
In Makefiles, variables longer than a single character (i.e. all variables that you're likely to define) needs to be expanded with ${varname}, not $varname. The latter would result in the value of $v concatenated with the string arname, as you discovered.
I won't start to parse the rest of that Makefile as the piping looks a bit questionable.

need to parse between second underscore and first hyphen of the text using sed

I have an rpm file, e.g. abc_defg_hijd-3.29.0-2_el6_11h.txt.
I need to parse the words between the 2nd underscore _ and first hyphen - of the above text,
so the required output will be hijd.
I was able to parse the above with sed for the above, but it worked only for the above example and I have filenames which differ a little, hence I would like to explicitly parse between the second underscore and first hyphen.
Use this sed command (on Mac):
sed -E 's/^[^_]*_[^_]*_([^-]*)-.*$/\1/'
OR (on Linux):
sed -r 's/^[^_]*_[^_]*_([^-]*)-.*$/\1/'
Using awk:
awk -F '_' '{sub(/-.*$/, "", $3); print $3}'
$ foo='abc_defg_hijd-3.29.0-2_el6_11h.txt'
$ bar=${foo%%-*} # remove everything after the first -
$ bar=${bar#*_}; bar=${bar#*_} # remove everything before the second _
$ echo "${bar}"
hijd
grep was born to extract:
grep -oP '[^_-]*_\K[^_-]*(?=-)'
example
kent$ echo 'abc_defg_hijd-3.29.0-2_el6_11h.txt'|grep -oP '[^_-]*_\K[^_-]*(?=-)'
hijd
awk is nuclear bomb for text processing,but it can kill a fly for sure:
awk -F- 'split($1,a,"_")&&$0=a[3]'
or shorter(gawk):
awk -v FPAT="[^-_]*" '$0=$3'
example
kent$ echo 'abc_defg_hijd-3.29.0-2_el6_11h.txt'|awk -F- 'split($1,a,"_")&&$0=a[3]'
hijd
kent$ echo 'abc_defg_hijd-3.29.0-2_el6_11h.txt'|awk -v FPAT="[^-_]*" '$0=$3'
hijd
with GNU sed
echo 'abc_defg_hijd-3.29.0-2_el6_11h.txt' |
sed 's/\([^_]\+_\)\{2\}\([^-]\+\)-.*/\2/g'
hijd
windows batch:
for /f "tokens=3delims=_-" %%i in ("abc_defg_hijd-3.29.0-2_el6_11h.txt") do echo %%i
hijd

How to compose custom command-line argument from file lines?

I know about the xargs utility, which allows me to convert lines into multiple arguments, like this:
echo -e "a\nb\nc\n" | xargs
Results in:
a b c
But I want to get:
a:b:c
The character : is used for an example. I want to be able to insert any separator between lines to get a single argument. How can I do it?
If you have a file with multiple lines than you want to change to a single argument changing the NEWLINES by a single character, the paste command is what you need:
$ echo -en "a\nb\nc\n" | paste -s -d ":"
a:b:c
Then, your command becomes:
your_command "$(paste -s -d ":" your_file)"
EDIT:
If you want to insert more than a single character as a separator, you could use sed before paste:
your_command "$(sed -e '2,$s/^/<you_separator>/' your_file | paste -s -d "")"
Or use a single more complicated sed:
your_command "$(sed -n -e '1h;2,$H;${x;s/\n/<you_separator>/gp}' your_file)"
The example you gave is not working for me. You would need:
echo -e "a\nb\nc\n" | xargs
to get a b c.
Coming back to your need, you could do this:
echo "a b c" | awk 'OFS=":" {print $1, $2, $3}'
it will change the separator from space to : or whatever you want it to be.
You can also use sed:
echo "a b c" | sed -e 's/ /:/g
that will output a:b:c.
After all these data processing, you can use xargs to perform the command you want to. Just | xargs and do whatever you want.
Hope it helps.
You can join the lines using xargs and then replace the space(' ' ) using sed.
echo -e "a\nb\nc"|xargs| sed -e 's/ /:/g'
will result in
a:b:c
obviously you can use this output as argument for other command using another xargs.
echo -e "a\nb\nc"|xargs| sed -e 's/ /:/g'|xargs

Resources