create SQL-statements from textfile with awk

2010-03-08 1 min read Linux

<a href="http://feedproxy.google.com/~r/Command-line-fu/~3/lz9uEhVxEEk/create-sql-statements-from-textfile-with-awk">create SQL-statements from textfile with awk

  <td>
    <div class="text codecolorer">
      $ $ awk \'{printf "select * from table where id = %c%s%c;\\n",39,$1,39; }\' inputfile.txt
    </div>
  </td>
</tr>
1

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 \’

    <td>
      <div class="text codecolorer">
        1 = first column
      </div>
    </td>
  </tr>
</table>

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

Continue reading

Bash histoy – common history in different terminals

2010-03-04 1 min read Bash Linux

I have been looking to do this for sometime. The bash history is per
session basis and the last session to quit overwrites the bash_history
file and thus all other terminals started between the time last terminal
lasted does not get to write the commands in the history file. Secondly
the history gets cluttered with lot of duplicate entries and entries
with ls and cd commands. So finally I added these to the bashrc file:

Continue reading

Bash Sub Shells

2010-03-02 1 min read Bash

Taken from <a href="http://www.linuxjournal.com/content/bash-sub-shells" target="_blank">here.

<span class=\"c\">#!/bin/bash</span>

<span class="nv">server_cmd</span><span class="o">=</span>server <span class="nv">pid_file</span><span class="o">=</span><span class="k">$(</span>basename <span class="nv">$server_cmd</span> .sh<span class="k">)</span>.pid <span class="nv">log_file</span><span class="o">=</span><span class="k">$(</span>basename <span class="nv">$server_cmd</span> .sh<span class="k">)</span>.log

<span class="o">(</span> <span class="nb">echo</span> <span class="s2">"Starting server"</span> <span class="nb">echo</span> <span class="s2">"Doing some init work"</span> <span class="nv">$server_cmd</span> <span class="c"># server becomes a daemon</span>

&lt;span class=\"k\">while &lt;/span>&lt;span class=\"nb\">true&lt;/span>

<span class="nb"> </span><span class="k">do</span> <span class="k"> if</span> <span class="o">[[</span> -f <span class="nv">$pid_file</span> <span class="o">]]</span>; <span class="k">then</span> <span class="k"> </span>sleep 15 <span class="k">else</span> <span class="k"> </span><span class="nb">break</span> <span class="nb"> </span><span class="k">fi</span> <span class="k"> done</span> <span class="k"> </span>mail -s <span class="s2">"Server exitted"</span> joe@blow.com <<<CRAP

Continue reading

bash script with sql to get the number of records from multiple tables.

2010-02-10 1 min read Bash Database Learning Linux Solaris

Here is the bash script:

#!/bin/sh
names[1]=errorlog
names[2]=amit1log
names[3]=amit2log
names[4]=amit3log
names[5]=amit4log
j=1
echo $1
for i in $( sqlplus amit/passwd@tns @get_count.sql |sed  -n &#8217;/COUNT/,/Disconnected/p&#8217;|sed &#8217;/COUNT/ d&#8217;|sed &#8217;/—/ d&#8217;|sed &#8217;/Disconnected/ d&#8217;|tr &#8217;n&#8217; &#8217; &#8217;  )
do
temp=${names[$j]}
let count=30-${#temp}
for ((I=1; I <= $count ; I++))
do
printf &#8221; &#8221;
done
echo  &#8221;${names[$j]}    : $i&#8221;
# echo $j
let j=j+1
done
echo
echo

and the required sql script:

select count() from errorlog;
select count(
) from amit1log;
select count() from amit2log;
select count(
) from amit3log;
select count(*) from amit4log;
quit;

Continue reading

Bash styling(themeing), the stylist way.

2010-02-04 1 min read Bash

For last couple of days, I have been looking for solutions to make stlying my bash easier. So, first a screenshot of the effort:

<img class="size-full wp-image-703" title="bash style" src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/a.jpg" alt="bash style" width="511" height="332" />

I was looking for the simplest solutions to do this. So, I installed two packages to make my life easier :  bashstyle and fortune.

sudo yum install fortune\*

For bash-style you can download the source files <a href="http://www.nanolx.org/bashstyle-ng/" target="_blank">here. Download the file, then do the following:

Continue reading

grep -v with multiple patterns.

2010-01-25 1 min read Bash Linux

$ sed '/test/{/error|critical|warning/d}' somefile If you wanted to do all in one command, you could go w/ sed instead

  • <a href="http://www.commandlinefu.com/commands/view/3882/grep-v-with-multiple-patterns.">View this command to comment, vote or add to favourites
  • <a href="http://feeds2.feedburner.com/commands/by/pipping">View all commands by <a href="http://feeds2.feedburner.com/commands/by/pipping">pipping

<a href="http://www.commandlinefu.com"><img src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/header-logo.jpg" alt="" align="bottom" />

by David Winterbottom (<a href="http://codeinthehole.com">codeinthehole.com)

<a href="http://feedads.g.doubleclick.net/~a/J5B2Z5VTSlqhmd-2YMQ4ndmAu4U/0/da"><img src="http://blog.amit-agarwal.com/wp-content/uploads/img.zemanta.com/~a/J5B2Z5VTSlqhmd-2YMQ4ndmAu4U/0/di" alt="" align="bottom" />
<a href="http://feedads.g.doubleclick.net/~a/J5B2Z5VTSlqhmd-2YMQ4ndmAu4U/1/da"><img src="http://blog.amit-agarwal.com/wp-content/uploads/img.zemanta.com/~a/J5B2Z5VTSlqhmd-2YMQ4ndmAu4U/1/di" alt="" align="bottom" />

<img src="http://blog.amit-agarwal.com/wp-content/uploads/img.zemanta.com/~r/Command-line-fu/~4/LjKzo7FpzDU" alt="" width="1" height="1" align="bottom" />

URL: <a href="http://feedproxy.google.com/~r/Command-line-fu/~3/LjKzo7FpzDU/grep-v-with-multiple-patterns.">http://feedproxy.google.com/~r/Command-line-fu/~3/LjKzo7FpzDU/grep-v-with-multiple-patterns.<h6 class="zemanta-related-title" style="font-size: 1em;">Related articles by Zemanta <ul class="zemanta-article-ul"> <li class="zemanta-article-ul-li"><a href="http://www.seroundtable.com/archives/020935.html">Google&#8217;s &#8221;Show More Results&#8221; Plus Box (seroundtable.com) <li class="zemanta-article-ul-li"><a href="http://www.macworld.com/article/143351/2009/10/netprocesses.html?lsrc=rss_main">See which processes are using the Internet (macworld.com) <div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/cc60245b-7397-4d48-83b2-7bfb345ae7a9/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/reblog_e20.png" alt="Reblog this post [with Zemanta]" /><span class="zem-script more-related pretty-attribution">

Continue reading
1