This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 4 years ago.
I have two variables in a bash script
hostname="ab78ascsoadp003.abc.com"
Loc=`$hostname | cut -c3,4`
I am getting an error ab78ascsoadp003.abc.com: command not found
I am trying to use cut command so that $Loc gets 78
While you can use cut to achieve this, sometimes it is useful to stick to bash:
hostname="ab78ascsoadp003.abc.com"
Loc=${hostname:3:2}
${parameter:offset:length} Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions <snip>
source: man bash
hostname="ab78ascsoadp003.abc.com"
Loc=$(cut -c3,4 <<<"$hostname")
you are missing an echo
Loc=`echo $hostname | cut -c3,4`
Related
This question already has answers here:
Concat numbers from JSON without doublequotes using jq [duplicate]
(1 answer)
How to remove double-quotes in jq output for parsing json files in bash?
(2 answers)
Closed 1 year ago.
I have:
MY_FOLDER=`jq '.results_folder' ./conf.json`
FOLDER_WITHOUT_QUOTES=$MY_FOLDER | sed 's/"//g'
python my_code.py > $FOLDER_WITHOUT_QUOTES/log.log
So, there is a json file with a folder name. But jsons demand strings to be inside ". And reading the json with bash returns me ", which I want to remove
Passing it to a variable and then applying sed isn't working. What's the correct syntax for doing it?
Thank you!
Posix shell would need:
FOLDER_WITHOUT_QUOTES="$(printf '%s\n' "$MY_FOLDER" | sed 's/"//g')"
With Bash you can use the here-document syntax:
FOLDER_WITHOUT_QUOTES=$(sed 's/"//g' <<< "$MY_FOLDER")
... and you can even get rid off a call to sed with the special substitution:
FOLDER_WITHOUT_QUOTES=${MY_FOLDER//\"}
Note: prefer the $(command) syntax to the backquotes which are less readable and cannot be nested as easily.
This question already has answers here:
Replace one substring for another string in shell script
(16 answers)
Closed 4 years ago.
I've been trying to wrap my head around this for over an hour now and my searches haven't helped yield the answer.
Trying to set a variable inside a bash script. This variable is taking variable-A and removing variable-B from it.
Prefix="$(echo ${Process} | sed -e 's/${Server}//g')"
So if Process=abcd1wxyz01 and Server=wxyz01, then Prefix should end up being abcd1.
I've tried so many iterations from online searches I honestly can't recall what all I've tried.
Your problem are the quotes, as pointed out in afsal_p's answer.
You could do this with parameter expansion instead:
$ process=abcd1wxyz01
$ server=wxyz01
$ prefix=${process%"$server"}
$ echo "$prefix"
abcd1
The ${word%suffix} expansion removes suffix from the end of word.
please use " instead of ' while using bash variables inside sed:
Prefix="$(echo ${Process} | sed -e "s/${Server}//g")"
echo $Prefix
This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 1 year ago.
I have a shell script:
TOPDIR=`pwd`
FOLDER=$($TOPDIR | sed 's/\//\_/g')
if [[ condition ]];then
source ~/[$FOLDER]-build/build-env.sh
fi
the TOPDIR here is /home/uname/project, so the variable FOLDER is supposed to be _home_uname_project because sed is called to replace / with _.
But it goes wrong when executing, terminal tells that /home/uname/[]-build/build-env.sh: No such file or directory which, I guess, means that FOLDER is unexpected empty in the if-then statement. Can anybody help me with figuring this out?
If you look at the output of just
$TOPDIR | sed 's/\//\_/g'
you'll realize that it's empty; it's trying to execute a command equal to the contents of $TOPDIR and pipe the output of that into sed, but there is no output in the first place.
You could do
pwd | sed 's\//_/g'
instead (no need to escape _), which would work.
Or, instead of using an external tool, you could use parameter expansion
topdir="$(pwd)"
topdir="${topdir//\//_}"
with the same result.
Notice that uppercase variable names are discouraged, as they're more likely to clash with existing, reserved names.
This question already has answers here:
Environment variable substitution in sed
(12 answers)
SED not working [unterminated `s' command]
(3 answers)
Closed 7 years ago.
I have a variable called "num", that holds a number. I want to use it in "sed" like so:
sed '1,$(num)d' file.txt
also tried:
sed '1,($num)d' file.txt
...so that I can delete lines from 1 until the line num.
It gives me an error like this:
sed: -e expression #1, char 4: unknown command: `('
What is the correct way to do this? thank.
Your shell variable usage was incorrect. First, using the double quote ensures that the shell will expand the variable. Second, surrounding the variable in question (num) with the braces ensures that the variable will be seen by the shell as $num, instead of the subsequent d getting glommed on.
Here is how you should specify what you want to do:
sed "1,${num}d" file.txt
You can use single quotes but need to concatenate the script
num=42; seq 1 45 | sed '1,'$num'd'
will print
43
44
45
as expected.
This question already has answers here:
Can ${var} parameter expansion expressions be nested in bash?
(15 answers)
Closed 7 years ago.
I have a variable named inet which contains the following string:
inet="inetnum: 10.19.153.120 - 10.19.153.127"
I would like to convert this string to notation below:
10.19.153.120 10.19.153.127
I could easily achieve this with sed 's/^inetnum: *//;s/^ -//', but I would prefer more compact/elegant solution and use bash. Nested parameter expansion does not work either:
$ echo ${${inet//inetnum: /}// - / }
bash: ${${inet//inetnum: /}// - / }: bad substitution
$
Any other suggestions? Or should I use sed this time?
You can only do one substitution at a time, so you need to do it in two steps:
newinet=${inet/inetnum: /}
echo ${newinet/ - / }
Use a regular expression in bash as well:
[[ $inet =~ ([0-9].*)\ -\ ([0-9].*)$ ]] && newinet=${BASH_REMATCH[#]:1:2}
The regular expression could probably be more robust, but should capture the two IP addresses in your example string. The two captures groups are found at index 1 and 2, respectively, of the array parameter BASH_REMATCH and assigned to the parameter newinet.