Search for specific string using shell script - linux

I have a set of log files and I need to find tables from a specific database being used by searching them in log files. The search keyword should start with 'database.' and should be able to grep/parse all table names with preceding database name. Can anyone advise on how this can be achieved using shell script.
Thanks

This is very easy:
grep "database" * : this shows the filename + the entire line
grep -l "database" * : this shows only the filename
grep -o "database [a-z]*" * : this shows the filename + the database name
grep -h -o "database [a-z]*" * : this shows the database name

Related

Insert data from STDIN to specific position of string

Case: I wanna insert some data from a file to specific position of the string.
Example:
cat users.log | mysql -h localhost -u mysql -e 'SELECT id FROM users WHERE ${USERS_IDS}'
I want to replace ${USERS_IDS} string to the data from the file
I'm sure this case is very popular, but I did not find a suitable solution
To insert the contents of a file in a specific position of a string you use the facilities of your shell.
For example, Bash has the $(< filename) construction:
mysql -h localhost -u mysql -e 'SELECT id FROM users WHERE '"$(< users.log)"
If the data to be inserted needs to be editted a little, you have the $(command) construction:
mysql -h localhost -u mysql -e 'SELECT id FROM users WHERE '"$(< users.log sed -e 's/.../.../')"
Whether the file substitution or the command substitution are to be enclosed in doublequotes or not depends on the specific use case.
And, if the intended use is really to feed values in a MySQL command, beware of Bobby Tables.

Problem of running command from Rundeck (Linux)

On a linux server I am running following command with any error and getting the result.
xxxxx#server1 ~]$ grep -o "\-w.*%" /etc/sysconfig/nrpe-disk
-w 15% -c 7%
[xxxxx#server1 ~]$
I want to run same command from Rundeck's command line interface with same xxx user which has sudo rights too.
Command executed from rundeck gives option '.' invalide error:
option invalide -- '.'
Utilisation : grep [OPTION]... MOTIF [FICHIER].
I tried many times with different ways such as escaping . sign, running it with sudo, with absolute path, double quotes - single quotes etc. Still I am receiving same output however, in the server command works locally. What's the way to fix it ?
You can do that putting that on an inline-script ("Script" step) or call an external script with the command content ("Script file or URL" step).
Another way is to use cat tool to print the file and capture the output using log filter (Click on the tiny Gear icon at the left of the step > Click on "Add Log Filter" > Select "Key/value data" and in pattern use with this regex: .*(-w .*%).*, put a name of the data - eg: diskdata - and click on "Log data" checkbox) and you get the output that you want, you can print that value using echo ${data.diskdata} in next step. Check.

Error retreiving database name wordpress

I was trying the below code to retrieve database name from wordpress configuration file. But somehow a single inverted comma at the beginning is causing trouble.
db=`grep DB_NAME /var/www/vhosts/domain.com/httpdocs/wp-config.php | tr -d "(',);" | cut -d" " -f2`;
echo $db;
mysqldump -uadmin -p$(cat > /etc/psa.psa.shadow) $db > $db.sql
As given below it shows the output without any special characters but the below error shows an issue.
laysalaysa_laysalaysa
'" when selecting the databasencorrect database name 'laysalaysa_laysalaysa'
carriage return (\r) caused the issue. Truncated it and script worked. But could not identify what cause the same.

Append current date to the filename via Cron?

I've created a Cron task at my webhost to daily backup my database and I would like it to append the current date to the filename.
My Cron job looks like this
mysqldump -u username -pPassword db_name > www/db_backup/db_backup+date%d%m%y.sql
But the file I get is this: db_backup+date no file extension or date.
I've also tried this command
mysqldump -u username -pPassword db_name > www/db_backup/db_backup_'date +%d%m%y'.sql
but that doesn't even give an file output.
What is the right syntax for getting the date appended to my file??
* * * * * echo "hello" > /tmp/helloFile_$(date +\%Y\%m\%d\%H\%M\%S).txt
You just need to escape the percent signs.
Other date formats:
http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
You should use `` instead of '' around the command you want to execute to generate the current date for your filename.
You must escape the format and use evaluation
mysqldump -u username -pPassword db_name > www/db_backup/db_backup_`date +\%d\%m\%y`.sql
I need to create a new log file every time command is executed. So every day I should have a log like this /home/me/Logs/power_20151230.log
The crontab line that I use is this:
00 8 * * * /home/me/power.py ON >> /home/me/Logs/power\_`date +20\%y\%m\%d`
Note the underscore character must be escaped too.

How Can I Import Data From Sql Server CE DataBase File To Excel?

How Can I Import Data From a Table in Sql Server CE To Excel?
I Try This From Excel But I Can Not Find an Item for This In Excel
Please Help Me To This
You can try the sqlcecmd command from the command line.
sqlcecmd -d "Data Source=C:\NW.sdf" -q "SELECT * FROM my_table" -h 0 -s "," -o my_data.csv
Of course you need to specify the correct location of your Data Source in the -d command
You need to specify the cells you wish to SELECT and the table in the FROM section of the -q command.
The "-h 0" flag will remove column names and any dashed lines.
The "-s ','" specifies the delimiter you wish you use between fields.
And finally the -o command specifies your output file.
You may also need to specify a username (-U) and password (-P) if these values have been set.
Before you can run the sqlcecmd you will need to make sure that the executable files for SQL Server CE are in your path.
Since you haven't specified a programming language i'm assuming you are doing this manually.
After you have the csv file excel should be able to open it no problem.
EDIT: If you do not have the SQLCECMD application then download and set it up first.

Resources