Unable to define line number in sed by variable - bash

I'm trying to use an array to define the lines to replace using sed; I can delete the lines using a variable for the line number but I can't get sed to use the variable to define the line number to write to. The problem seems to reside in the insert line. How do you pass the value of an array as a line number to sed?
#!/bin/bash
lineNum=$(sed -n '/max_allowed_packet/=' /etc/mysql/my.cnf)
IFS= #There's a space as the delimiter#
ary=($lineNum)
#for key in "${!ary[#]}";
# do
# sed -i '$ary[$key]'d /etc/mysql/my.cnf;
# #The folllowing line errors#
# sed -i "'$ary[$key]'imax_allowed_packet = 32M" /etc/mysql/my.cnf;
# #The above line errors#
#done
#for val in "${ary[#]}";
# do
# sed -i "${val}d" /etc/mysql/my.cnf;
# sed -i "${val}imax_allowed_packet = 32M" /etc/mysql/my.cnf;
# done
for val in "${ary[#]}";
do
sed -i "${val}s/.*/imax_allowed_packet = 32M" /etc/mysql/my.cnf";
done
For the first stanza of script I get the following output:
Error: sed: -e expression #1, char 1: unknown command: `''
For the second Stanza I get the following output:
sed: -e expression #1, char 3: unknown command:
'
sed: -e expression #1, char 3: unknown command:
'
For the third Stanza I get the following output:
./test.sh: line 22: unexpected EOF while looking for matching `"'
./test.sh: line 24: syntax error: unexpected end of file
Edit, rewriting the sed commands as sed -i "${ary[$key]}" generates the following error output: sed: -e expression #1, char 3: unknown command: `

I think you're over-complicating the issue. Your script can be reduced to this:
sed 's/\(max_allowed_packet\).*/\1 = 32M/' /etc/mysql.cnf
This performs a substitution on every occurrence of max_allowed_packet, setting the rest of the line to = 32M. Add the -i switch to overwrite the file when you're happy with the result.
Problems with your attempt
Shell parameters are not expanded within single quotes, so you would need to use double quotes, e.g. sed -i "${ary[$key]}d". You can use set -x to see what is happening here - at the moment, you will see the literal string $ary[$key], rather than the array value.
If I understand your intention correctly (you want to substitute the entire line), there's no need to call sed twice:
for val in "${ary[#]}"; do
sed -i.bak "${val}s/.*/imax_allowed_packet = 32M" /etc/mysql/my.cnf
done
I have chosen to loop through the values of the array, instead of the keys, in order to simplify things a little. When using the -i option, it is always a good idea to specify a backup file, as I have done.

Related

String not getting replaced with sed command

In my script, I am using below sed command
while read line; do
echo "$line"
sed -r "s/where /where $line ;/" Query.cql > Query-2.cql
done < $1
the value of $line is
where Column11 in ('Value1','Value2','Vlaue3') and Column12 in
('Value11','Value22','Vlaue32')
File Content ::
capture 'data.csv'
select * from test where
capture off;
After Executing
sed -r "s/where /where $line ;/" Query.cql > Query-2.cql
OUTPUT is ::
capture 'data.csv'
select * from test where
capture off;
Here, the string is not getting replaced.
What went wrong?
sed: -e expression #1, char 19: unterminated `s' command
Sure the s command is s/<something>/<else>/ - there is a trailing /. Do:
sed -r 's/where /$line ;/'
^ - trailing /
The -r option seems unused - where has no extended regular expressions. If so, remove it.
Your command uses ' quotes, so $line is not expanded. Research the difference between single and double quotes in shell, most probably you meant to use " here.
Note that each loop > qWithWhere.cql is recreating result and overwriting the result from previous loop. You might just run the loop on the last line them.
Read how to read a file line by line in bash and how to Escape a string for a sed replace pattern .
The following code with a space after where in input:
cat <<EOF >input
capture 'data.csv'
select * from test where
capture off;
EOF
line="where Column11 in ('Value1','Value2','Vlaue3') and Column12 in ('Value11','Value22','Vlaue32')"
sed -r "s/where /where $line ;/" input
outputs:
capture 'data.csv'
select * from test where where Column11 in ('Value1','Value2','Vlaue3') and Column12 in ('Value11','Value22','Vlaue32') ;
capture off;

Sed insert blank line below search string then insert string on the next line

I have a sed expression in a function that I pass parameters to.
insert_after_new_line() {
if ! grep -q "${2}" "${3}"; then
sed -i "/${1}/{N;N;a ${2}}" "${3}"
fi
}
insert_after_new_line search insert textfile.txt
I am trying to have a blank line inserted below the search string and the insert string inserted after.
so
text
text
search
text
becomes
text
text
search
insert
text
but I keep getting the error
sed: -e expression #1, char 0: unmatched `{'
I tested this. works in command line
sed -i '/search/a \\ninsert' file
sed really delimiters commands by a newline. There is a ; but it does not work for all commands, mostly these which take file name as an argument. ; does not work for r R or for example a. Sed will read everything after a command, so sed interprets is like a ${2}} as a single command, in result it does not find enclosing }, cause it's been eaten by a command. You need a newline:
sed -i "/${1}/{N;N;a ${2}
}" "${3}"
or
sed -i "/${1}/{N;N;a ${2}"$'\n'"}" "${3}"
this should work:
sed -i '/search/{G;ainsert
}' file
You can replace the text by shell variable, but replace the single quotes by double quotes too.

How to expand correctly a variable in sed command

I have this file:
user0:
hash: $2a$12$DA47ZutoC.89KDePyBK.yubz.2vfEiLi28ENBRuyoNf.px3.vHINq
user1:
hash: $2a$12$Auk82xqdbgFb4AJKyvSuWOKTQivuHhJZwckLii5/a5ILhCke0sddS
user3:
hash: $2a$12$QW/dCK7CqE5s87OIk8QBeeBYVvD5tjbd46jH9wVD9YMWW31KQEA1y
user4:
hash: $2a$12$60ds1ivJzM/DexIBYUilzO4BPjumsdTOETAHoopIxabffoWiDEkum
and a bash script to change the hash associated to a username.
For example to change user1 hash, i would use:
sed -i "/user1:/{n;s,.*, hash: NEW_HASH/}" MY_FILE
Now, in the script I have variables, and so actually my previous example is:
sed -i "/$1:/{n;s,.*, hash: $hash/}" "$users"
My problem is that the $hash parameter has got also / characters, resulting in a sed error like this:
sed: -e expression #1, char 85: unterminated `s' command
How to correct my command in order to let it expand hash variable correctly? I tried some combinations of ' and " commands but without results. Any help?
Thank you!
Since you are using , as the delimiter for the s command it should be:
sed -i "/$1:/{n;s,.*, hash: $hash,;}" "$users"
----------------------------------^ comma, not /
I've also appended a ; after the s command. Some implementations of sed need that.

Replace strings with variable in bash script

I want to create a bash script to change the configuration file of an application. The script will read a variable and replace the value of a setting that I want to change with the variable value.
#!/bin/bash
mysqlurlenv1="jdbc:mysl://xx.xx.xx.xx:3306
mysqlurlenv2="jdbc:mysl://xx.xx.xx.xz:3306
configFile=app.conf
pattern=$1
case "$pattern" in
1)
sed -i.bak "s/^\(mysql.db.url =\).*/\1 ${mysqldburl_env1}/"
;;
2)
sed -i.bak "s/^\(mysql.db.url =\).*/\1 ${mysqldburl_env2}/"
;;
*)
echo "Wrong input...try again"
esac
I have a config file with the following entry
mysql.db.driver = com.mysql.jdbc.Driver
mysql.db.url = jdbc:mysql....
mysql.db.uname = admin
When I run the script I got this error
sed: -e expression #1, char 40: unknown option to `s'
How to get those defined variable in the config file ? Any help will appreciated
Rgds
Din
You are using this sed pattern s/FIND_STRING/REPLACE_STRING, that is the common way to do it.
But your REPLACE_STRING contains /, so your pattern will become s/FIND_STRING/jdbc:mysl://.... and that is invalid, because it contains too much /.
Sed accepts any character as field separator, so in your case you need change sed pattern field separator from / to something else, like |.
Example:
$ mysqlurlenv1="jdbc:mysl://xx.xx.xx.xx:3306"
$ echo "mysql.db.url = jdbc:mysql" | sed "s|^\(mysql.db.url =\).*|\1 ${mysqlurlenv1}|"
mysql.db.url = jdbc:mysl://xx.xx.xx.xx:3306

"unterminated address regex" using variable in sed

I'm trying to use a variable in a sed append and hitting an issue.
The following command works as expected:
sed -i "\:#file = /mnt/var/log/hadoop-yarn/containers/application_1495965866386_0001/container_1495965866386_0001_01_000002/stderr:a file = /path/to/other/file" /etc/conf/service.conf
However if I replace the pattern with a variable I'm hitting an error:
$ echo $item
#file = /mnt/var/log/hadoop-yarn/containers/application_1495965866386_0001/container_1495965866386_0001_01_000002/stdout
$ sed -i "\:$item:a file = /path/to/other/file" /etc/conf/service.conf
sed: -e expression #1, char 122: unterminated address regex
EDIT for more info: So the 'item' variable is being populated from an array. That array is created from a readarray and grep:
$readarray LINES < <(grep "#file = /mnt/var/" /etc/conf/service.conf)
$item=${LINES[1]}
$echo $item
#file = /mnt/var/log/hadoop-yarn/containers/application_1495965866386_0001/container_1495965866386_0001_01_000002/stdout
However I've found if i populate 'item' manually it then works e.g:
$item="#file = /mnt/var/log/hadoop-yarn/containers/application_1495965866386_0001/container_1495965866386_0001_01_000002/stdout"
$sed -i "\:$item:a file = /path/to/other/file" /etc/conf/service.conf
$
So something strange seems to be happening with the readarray/grep
So the problem here turned out to be newline characters that were being pulled in as part of the grep.
This is why populating $item manually worked - no '\n'
Thanks to Ed Morton for pointing me in the right direction. While
echo "$item" | cat -v
did not show anything I added '-t' to the readarray command to trim newline characters:
$readarray -t LINES < <(grep "#file = /mnt/var/" /etc/conf/service.conf)
After that things worked as expected.

Resources