This post was badly required, just for me. Every time I have a write a script, I google. Its difficult to remember the various options of various commands. And most of the time its search, extract or replace. So here it goes
1. To split a string and extract a value
value=$(echo $line | awk '{split($0, splitValues, " "); print splitValues[1] }')
- Input to split is the value in the variable line.
- split command splits the string, executing the regular expression within quotes and stores in an array splitValues.
- print splitValues[1] returns the first element in array splitValues
- If you dont want to store the value then echo $line | awk '{split($0, splitValues, " "); print splitValues[1] }' will print it on the console
2. Find a matching pattern in a file and extract them to another file
grep -n 'match this' test.log > result.log
- grep searches the complete file test.log for the string mentioned in quotes
- all the lines which matches the pattern are extracted to result.log
- -n prefixes the line number of every match (-n is optional)
3. Print last few lines from a file
tail -n
- This command extract the number of lines of the specified file and prints it on the console
- The output can always be redirected to a file tail -10 output.txt > subset.txt
4. Executing an sql in shell script - There are few steps to achieve this
- Write a sql and store it in a file, say SelectQuery.sql
- The shell script to connect to database and execute the query in SelectQuery.sql looks something like this
#!/bin/bash
sqlplus -s -l /@ << EOF >> output.txt
@SelectQuery.sql
exit
EOF
while read line
do
echo $line
done < output.txt
errorCode=$? # checks if the last operation (sqlplus) was completed successfully or not
if [ ${errorCode} -ne 0 ]
then
echo "SQLPlus was unable to connect the DB with the supplied credentials"
else
echo "SQLPlus was connected successfully"
fi
So what does this script do ?
- Connects to database using sqlplus command
- output.txt is where the results of the query will be stored
- @SelectQuery.sql actually executes the query
- The while loop, iterates through each lines in the output.txt and prints them on console
- A small check to display an errors at the end