sed fails to match, delete and then insert - sed

I'm running the following sed commands to edit a my.cnf with specific parameters. However it gets to the last two values I need to check for and amend and fails to do them despite the syntax being no different to the previous values which were amended.
sed -i s/".*old_passwords=[0-9]*"/"# old_passwords=1"/g /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/innodb_buffer_pool_size=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\old_passwords=/ainnodb_buffer_pool_size=5G' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/innodb_additional_mem_pool_size=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\innodb_buffer_pool_size=/ainnodb_additional_mem_pool_size=100M' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/innodb_file_per_table/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\innodb_additional_mem_pool_size=/ainnodb_file_per_table' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/innodb_log_file_size=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\innodb_file_per_table/ainnodb_log_file_size=125M' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/innodb_flush_log_at_trx_commit=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\innodb_log_file_size=/ainnodb_flush_log_at_trx_commit=1' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/max_connections=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\innodb_flush_log_at_trx_commit=/amax_connections=500' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/query_cache_size=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\max_connections=/aquery_cache_size=256M' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/query_cache_type=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\query_cache_size=/aquery_cache_type=0' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/query_cache_limit=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\query_cache_type=/aquery_cache_limit=1M' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/table_cache=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\query_cache_limit=/atable_cache=256' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/thread_cache_size=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\table_cache=/athread_cache_size=4' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/innodb_flush_method=/d' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
sed -i '/\thread_cache_size=/ainnodb_flush_method=O_DIRECT' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf
The last two values I need to check for and set are:
thread_cache_size=4
innodb_flush_method=O_DIRECT
I'm running this against a pretty standard my.cnf file:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
max_connections=500
innodb_buffer_pool_size=256M
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
The resulting file looks like this:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
# old_passwords=1
innodb_buffer_pool_size=5G
innodb_additional_mem_pool_size=100M
innodb_file_per_table
innodb_log_file_size=125M
innodb_flush_log_at_trx_commit=1
max_connections=500
query_cache_size=256M
query_cache_type=0
query_cache_limit=1M
table_cache=256
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
I've tried removing the thread_cache_size= commands and replacing with others, random, ones which worked further up but it still fails, as if there is some limit as to how far into the my.cnf file it will edit.

\t has a special meaning in sed: it stands for the tab character. Therefore, /\table_cache/ never matches.

For a shell point of view, isn't it better to use this sed (and still better to use t file as action list)
sed -i 's/".*old_passwords=[0-9]*"/"# old_passwords=1"/g
/innodb_buffer_pool_size=/d
/\old_passwords=/ainnodb_buffer_pool_size=5G
/innodb_additional_mem_pool_size=/d
/\innodb_buffer_pool_size=/ainnodb_additional_mem_pool_size=100M
/innodb_file_per_table/d
/\innodb_additional_mem_pool_size=/ainnodb_file_per_table
/innodb_log_file_size=/d
/\innodb_file_per_table/ainnodb_log_file_size=125M
/innodb_flush_log_at_trx_commit=/d
/\innodb_log_file_size=/ainnodb_flush_log_at_trx_commit=1
/max_connections=/d
/\innodb_flush_log_at_trx_commit=/amax_connections=500
/query_cache_size=/d
/\max_connections=/aquery_cache_size=256M
/query_cache_type=/d
/\query_cache_size=/aquery_cache_type=0
/query_cache_limit=/d
/\query_cache_type=/aquery_cache_limit=1M
/table_cache=/d
/\query_cache_limit=/atable_cache=256
/thread_cache_size=/d
/\table_cache=/athread_cache_size=4
/innodb_flush_method=/d
/\\thread_cache_size=/ainnodb_flush_method=O_DIRECT' /home/david/systems/zabbix/2.2/2.2.5/confs/my.cnf

Related

How do I add the sed command inside a bash script?

I want to add below line to build.sh file to a line number 26
sed -i 's/-DskipTests //' dev/make-distribution.sh
I tried with this command
sed "26 a sed -i 's/-DskipTests //' dev/make-distribution.sh" build.sh
But this is giving error
sed: 1: "26 a sed -i 's/-DskipTe ...": command a expects \ followed by text`
Try putting it in as if it were multiline add as a workaround. Does this work for you?
sed "26 a\\sed -i 's/-DskipTests //' dev/make-distribution.sh" build.sh
You need to quote your quote since you are compounding quote-types.
c.f. the manual, though - it ought to work as it is.

Sed remove matching lines script

I'm requesting help with a very simple script...
#!/usr/bin/sed -f
sed '/11,yahoo/d'
sed '/2506,stackover flow/d'
sed '/2536,reddit/d'
Just need it to remove three matches that account for 18408 in my file, data.csv
% sed -f remove.sed < data.csv
sed: 3: remove.sed: unterminated substitute pattern
Doing these same lines individually is no problem at all, so what am I doing wrong with this?
Using freeBSD 10.1 and its implementation of sed, if that matters.
This, being a sed script, should not have "sed" at each line.
Either change it to:
#!/usr/bin/sed -f
/11,yahoo/d
/2506,stackover flow/d
/2536,reddit/d
Or to
#!/bin/sh
sed -e /11,yahoo/d \
-e /2506,stackover flow/d \
-e /2536,reddit/d

how to replace softtabs with hardtabs in sed

I am trying to replace softtabs with hardtabs in sed. I have tried the following but to no avail:
sed -i 's/ /\t/g' path/to/file
What am I doing wrong?
It appears what I was looking for was the unexpand command.
unexpand -a -t4 file > newfile
If you have a file like this where all space is spaces and not tabs:
cat file
test more data
here are more
You can use
sed 's/ */\t/g'
or
sed -r 's/ +/\t/g'
and get
test more data
here are more
Where it now have changed multiple spaces to tab

Search replace regular expression variable using sed

This is probably a trivial one:
I have a file (my.file) with these lines:
>h1_c1
>h1_c2
>h1_c3
>h2_c1
>h2_c2
>h2_c3
and I want to change it in place to be:
>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h3
I thought this ought to do it:
sed -i 's/\(\>\)\(h1\)\(\_\)\(.*\)/\1 \4 \3 \2/g' my.file
sed -i 's/\(\>\)\(h2\)\(\_\)\(.*\)/\1 \4 \3 \2/g' my.file
but it doesn't seem to work. How do I do it?
The obvious sed for your example is:
$ sed -i~ -e 's/^>\(h[0-9]\)_\(c[0-9]\)/>\2_\1/' *.foo
I tested this and it works for your example file.
Try this awk
awk -F">|_" '{print ">"$3"_"$2}' my.file > tmp && mv tmp my.file
awk -F">|_" '{print ">"$3"_"$2}' my.file
>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h2
You can try this sed,
sed 's/>\(h[1-2]\)_\(.*\)/>\2_\1/' yourfile
(OR)
sed -r 's/>(h[1-2])_(.*)/>\2_\1/' yourfile
kent$ sed -r 's/>([^_]*)_(.*)/>\2_\1/' f
>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h2
you add -i if you want it to happen "in-place"

sed: can't read /home/me/weather: No such file or directory

I have the following extract from a script that fetches weather information from accuweather:
wget -O ./weather_raw $address
if [[ -s ./weather_raw ]]; then
egrep 'Currently|Forecast<\/title>|_31x31.gif' ./weather_raw > ./weather
sed -i '/AccuWeather\|Currently in/d' ./weather
sed -i -e 's/^[ \t]*//g' -e 's/<title>\|<\/title>\|<description>\|<\/description>//g' ./weather
sed -i -e 's/<img src="/\n/g' ./weather
sed -i '/^$/d' ./weather
sed -i -e 's/_31x31.*$//g' -e 's/^.*\/icons\///g' ./weather
sed -i -e '1s/.$//' -e '3s/.$//' -e '6s/.$//' ./weather
for (( i=2; i<=8; i+=3 ))
do
im=$(sed -n ${i}p ./weather)
sed -i $i"s/^.*$/$(test_image $im)/" ./weather
done
fi
The command that triggers the code above is in a conkyrc file and its ~/.conkyblue/accu_weather/rss/acc_rss. When I run the conkyrc script from the prompt, I get an error
sed: can't read /home/me/weather: No such file or directory
And indeed when I check, the "weather" file is not created. However if run the command ~/.conkyblue/accu_weather/rss/acc_rss directly from the prompt, it works as expected and create and puts content into the /home/me/weather file.
I don't know anything about the sed command although I'm trying to learn it as a result of this bother.
What could be the problem with the code. I don't think its a permission issue since the folder its writing into is my home folder and I of-course own it.
Thanks
It should have been created by egrep.
When you run your script, the weather directory will be created in the pwd of the script process.
Check and see why egrep does not create the file, or in which directory it created it.