{"id":759,"date":"2007-11-22T16:07:40","date_gmt":"2007-11-22T21:07:40","guid":{"rendered":"http:\/\/softbeam.net\/hobby\/?p=759"},"modified":"2007-11-22T16:07:40","modified_gmt":"2007-11-22T21:07:40","slug":"several-good-sh-command","status":"publish","type":"post","link":"https:\/\/softbeam.net\/hobby\/?p=759","title":{"rendered":"several very useful sh commands"},"content":{"rendered":"<p><code>fgrep -r \"something\" . (or *)<\/code><\/p>\n<p><code>tail -f (or a number) somefile<\/code><\/p>\n<p>cat file1 &gt;&gt; file2<\/p>\n<p>tar zcfv filename.tar.gz somedir<\/p>\n<p>tar zxfv filename.tar.gz<\/p>\n<p>telnet hostname portnumber<\/p>\n<p>a2ps -o &#8216;outfile.pdf&#8217; &#8216;infile&#8217;<\/p>\n<p>mysqladmin -u root password &#8216;root password for mysql&#8217;<\/p>\n<p>\/\/or after mysql -u root<\/p>\n<p>mysql&gt;SET PASSWORD FOR root@localhost=PASSWORD(&#8216;rootpassword&#8217;);<\/p>\n<p>sed:<\/p>\n<p>this command is great. I been using it to make my almost 300 web files be recognized as utf-8 in 1 second so that no bizzard charater showing on the webpage if you don&#8217;t change the charcter encoding:<\/p>\n<p><strong><code>for i in *; do sed \"s\/&lt;head&gt;\/&lt;head&gt;&lt;meta http-equiv=\"Content-type\" content=\"text\\\/html; charset=utf-8\"&gt;\/' $i &gt; tmp; mv tmp $i; done;<\/code><\/strong><\/p>\n<p><span class=\"a3\">If we want to replace all the &#8220;old&#8221; with &#8220;new&#8221; for a file &#8220;test&#8221;. We can do this by doing the following:<\/span><\/p>\n<p><tt> sed -e 's\/old\/new\/g' test &gt; test2 mv \u2013f test2 test <\/tt><\/p>\n<p><span class=\"a3\">This one deletes empty lines or lines that only contain spaces:<\/span><\/p>\n<p><tt> sed -e '\/^ *$\/d' sedtest1.txt<\/tt><\/p>\n<p>This one replace foo with foo_bar in multiple file:<\/p>\n<p><code>sed -i 's\/foo\/foo_bar\/g'  *.module<\/code><\/p>\n<p>This one is pretty dangerous:<\/p>\n<p>#!\/bin\/bash<\/p>\n<p><span name=\"intelliTxt\" id=\"intelliTxt\">sed -f sedscript filename.txt &gt; tempfile<br \/>\nmv tempfile filename.txt<\/span><\/p>\n<p>with sedscript file as follows:<\/p>\n<p><span name=\"intelliTxt\" id=\"intelliTxt\">s\/Apples\/Oranges\/g<\/span><\/p>\n<p>better replace the shell script like this:<\/p>\n<p><span name=\"intelliTxt\" id=\"intelliTxt\"><\/span><\/p>\n<pre class=\"alt2\" style=\"border: 1px inset ; margin: 0px; padding: 6px; overflow: auto; width: 640px; height: 162px\">#!\/usr\/bin\/shmkdir ${SAVED:=saved} || exit 1for files in `find . -type f`\n\ndo sed -f sedfile $file &gt;qqfile\n\n[ -s qqfile ] || continue # sed error, NO output\n\ncp $file $SAVED\/`echo $file|sed 's\/\\\/\/---\/g'`\n\ncp qqfile $file\n\ndone<span name=\"intelliTxt\" id=\"intelliTxt\"><\/span><\/pre>\n<p><code><\/code>awk or gawk examples from http:\/\/www.ss64.com\/bash\/gawk.html:<\/p>\n<p class=\"body\">This program prints the length of the longest input line:<\/p>\n<pre> awk '{ if (length($0) &gt; max) max = length($0) }      END { print max }' data<\/pre>\n<p><span class=\"body\">  This program prints every line that has at least one field.  This   is an easy way to delete blank lines from a file (or rather, to<br \/>\ncreate a new file similar to the old file but from which the blank   lines have been deleted)<\/span><\/p>\n<pre> awk 'NF &gt; 0' data<\/pre>\n<p><span class=\"body\">  This program prints seven random numbers from zero to 100,   inclusive.<\/span><\/p>\n<pre> awk 'BEGIN { for (i = 1; i &lt;= 7; i++)                print int(101 * rand()) }'<\/pre>\n<p><span class=\"body\">  This program prints the total number of bytes used by FILES.<\/span><\/p>\n<pre> ls -lg FILES | awk '{ x += $5 } ; END { print \"total bytes: \" x }'<\/pre>\n<p><span class=\"body\">  This program prints a sorted list of the login names of all users.<\/span><\/p>\n<pre> awk -F: '{ print $1 }' \/etc\/passwd | sort<\/pre>\n<p><span class=\"body\">  This program counts lines in a file.<\/span><\/p>\n<pre> awk 'END { print NR }' data<\/pre>\n<p><span class=\"body\"> This program prints the even numbered lines in the data file. If you were to use the expression `NR % 2 == 1&#8242; instead, it would print the odd numbered lines.<\/span><\/p>\n<pre> awk 'NR % 2 == 0' data<\/pre>\n<p><span class=\"a3\"><tt><\/tt><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>fgrep -r &#8220;something&#8221; . (or *) tail -f (or a number) somefile cat file1 &gt;&gt; file2 tar zcfv filename.tar.gz somedir tar zxfv filename.tar.gz telnet hostname portnumber a2ps -o &#8216;outfile.pdf&#8217; &#8216;infile&#8217; mysqladmin -u root password &#8216;root password for mysql&#8217; \/\/or after mysql -u root mysql&gt;SET PASSWORD FOR root@localhost=PASSWORD(&#8216;rootpassword&#8217;); sed: this command is great. I been using &hellip; <a href=\"https:\/\/softbeam.net\/hobby\/?p=759\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;several very useful sh commands&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-759","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=\/wp\/v2\/posts\/759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=759"}],"version-history":[{"count":0,"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=\/wp\/v2\/posts\/759\/revisions"}],"wp:attachment":[{"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softbeam.net\/hobby\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}