remove/replace text/path in config file.

Lot of times I end up changing a particular text in config file to some other text. And when I have to do it for multiple files, all files having the text in multiple places, I end up opening the files in vim and then doing a globlal replace. But this is not efficient so I cam up with some one liners to do this for me. For removing any occurance of text in all files in the directory :

for i in $(grep -l \’text-to-remove\’  *) ; do sed \’s@text-to-remove@@\’ $i >${i}.new; mv $i{,.bak}; mv $i{.new,}; done

And if you want to replace the text then the same command with little change in sed command:

for i in $(grep -l \’text-to-replace\’ *) ; do sed \’s@text-to-replace@new-text@\’ $i >${i}.new; mv $i{,.bak}; mv $i{.new,}; done

create SQL-statements from textfile with awk

create SQL-statements from textfile with awk

1
$ $ awk \'{printf "select * from table where id = %c%s%c;\\n",39,$1,39; }\' inputfile.txt

inputfile.txt is a space-separated textfile, 1st column contains the items (id) I want to put into my SQL statement.

39 = charactercode for single tick \’

1
1 = first column

If inputfile.txt is a CSV-file separated by \”,\” use FS= to define your own field-separator:

1
awk \'BEGIN {FS=","; }{printf "select * from table where id = %c%s%c;\\n",39,$1,39; }\' inputfile.txt

\"commandlinefu.com\"

by David Winterbottom (codeinthehole.com)

\"\"

\"\"

\"\"

URL: http://feedproxy.google.com/~r/Command-line-fu/~3/lz9uEhVxEEk/create-sql-statements-from-textfile-with-awk

Execute mysql command from shell

phpMyAdmin is a very slow application if you want to execute a query on your database. If you know the name of the database then any GUI tool is an overhead. So I have written a one liner shell script to do that for me 🙂

Here\’s the script:

echo \”select * from table where field like \”%$1%\”\”|mysql -h doamin.com -u amit -p -D amit -r –password=hello

When run with one argument it will execute the sql query directly without asking for password also.