using makefile variable in sed command [duplicate] - bash

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.

Related

User input into variables and grep a file for pattern

H!
So I am trying to run a script which looks for a string pattern.
For example, from a file I want to find 2 words, located separately
"I like toast, toast is amazing. Bread is just toast before it was toasted."
I want to invoke it from the command line using something like this:
./myscript.sh myfile.txt "toast bread"
My code so far:
text_file=$1
keyword_first=$2
keyword_second=$3
find_keyword=$(cat $text_file | grep -w "$keyword_first""$keyword_second" )
echo $find_keyword
i have tried a few different ways. Directly from the command line I can make it run using:
cat myfile.txt | grep -E 'toast|bread'
I'm trying to put the user input into variables and use the variables to grep the file
You seem to be looking simply for
grep -E "$2|$3" "$1"
What works on the command line will also work in a script, though you will need to switch to double quotes for the shell to replace variables inside the quotes.
In this case, the -E option can be replaced with multiple -e options, too.
grep -e "$2" -e "$3" "$1"
You can pipe to grep twice:
find_keyword=$(cat $text_file | grep -w "$keyword_first" | grep -w "$keyword_second")
Note that your search word "bread" is not found because the string contains the uppercase "Bread". If you want to find the words regardless of this, you should use the case-insensitive option -i for grep:
find_keyword=$(cat $text_file | grep -w -i "$keyword_first" | grep -w -i "$keyword_second")
In a full script:
#!/bin/bash
#
# usage: ./myscript.sh myfile.txt "toast" "bread"
text_file=$1
keyword_first=$2
keyword_second=$3
find_keyword=$(cat $text_file | grep -w -i "$keyword_first" | grep -w -i "$keyword_second")
echo $find_keyword

Unable to echo variable value

Assigning the output of sed command in a variable but unable to print its value, the command works fine:-
uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
but I assigned a variable for its out like below:-
abc=uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
and calling variable is not pulling the value.
Tried like below:-
echo {"$abc"}
printf "$abc"
echo "${abc}"
Kindly suggest the syntax for output.
abc=uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
Actually we need to pull the uptime value for only number of days on AIX server and call that value to form a report of servers which will show number of days server uptime for AIX servers. Need to know how to call the variable value and embedd it in a shell script.
Depends on your shell, but for most sh-ish variants:
abc=$(uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g')
Because you are not storing the final evaluation in abc.
Try
abc=$(uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g')

Removing word from grep output [duplicate]

This question already has answers here:
Parsing variables from config file in Bash
(7 answers)
Closed 4 years ago.
I have the following bash command:
cat setup.py | grep name=`
This returns the line
name='SOME_PROJECTNAME',
How would I pipe this output from grep to just retrieve SOME_PROJECTNAME?
I have tried
cat setup.py | grep name= | tr -d 'name=','
but this removes characters in SOME_PROJECTNAME.
Use grep lookahead.
$ grep -oP "(?<=name=').*(?=')" setup.py
#bad cat setup.py | grep name= | cut -d= -f2-
cat setup.py | grep name= | cut -d' -f2
Sed may be helpful here.
sed -ne "s/name='\(.*\)'/\1/p" setup.py
The option -n makes sed not print lines by default. Then we replace the entire property line (name='SOME_PROJECTNAME') with the value only (SOME_PROJECTNAME). The p flag in the s/// command makes sed print the line only if the replacement its executed. So, the only line to be printed are the ones where the replacement was done, with the replaced value.

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 a bash variable to pass multiple -e clauses to sed [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 6 years ago.
I'm creating a variable from an array which build up multiple -e clauses for a sed command.
The resulting variable is something like:
sedArgs="-e 's/search1/replace1/g' -e 's/search2/replace2/g' -e 's/search3/replace3/g'"
But when I try to call sed with this as the argument I get the error sed: -e expression #1, char 1: unknown command: ''
I've tried to call sed the following ways:
cat $myFile | sed $sedArgs
cat $myFile | sed ${sedArgs}
cat $myFile | sed `echo $sedArgs`
cat $myFile | sed "$sedArgs"
cat $myFile | sed `echo "$sedArgs"`
and all give the same error.
UPDATE - Duplicate question
As has been identified, this is a 'quotes expansion' issue - I thought it was something sed specific, but the duplicate question that has been identified put me on the right track.
I managed to resolve the issue by creating the sedArgs string as:
sedArgs="-e s/search1/replace1/g -e s/search2/replace2/g -e s/search3/replace3/g"
and calling it with:
cat $myFile | sed $sedArgs
which works perfectly.
Then I took the advice of tripleee and kicked the useless cat out!
sed $sedArgs $myFile
also works perfectly.
Use BASH arrays instead of simple string:
# sed arguments in an array
sedArgs=(-e 's/search1/replace1/g' -e 's/search2/replace2/g' -e 's/search3/replace3/g')
# then use it as
sed "${sedArgs[#]}" file
Here is no sane way to do that, but you can pass the script as a single string.
sedArgs='s/search1/replace1/g
s/search2/replace2/g
s/search3/replace3/g'
: then
sed "$sedArgs" "$myFile"
The single-quoted string spans multiple lines; this is scary when you first see it, but perfectly normal shell script. Notice also how the cat is useless as ever, and how the file name needs to be quoted, too.

Resources