Shiva's Weblog

Various things I am putting up for my reference. Hope its useful to you as well.

challa.net | Tech weblog |  Lyrics

Unix-related tid bits

By Shiva Challa

Friday, December 12, 2014
 Unix related
 $ ls -l ## show list of files and directories.   
total 447
drwxrwxrwx 5 schalla challanet 2017 Dec 12 07:44 dir1
-rw-r--r-- 1 schalla challanet 94056 Dec 12 09:33 file1.csv
-rw-r--r-- 1 schalla challanet 37880 Dec 11 14:38 file2.csv
drwxr-xr-x 2 schalla challanet 36 Dec 12 05:34 dir2
$ ls -ltr ## l = show List, t = Sort by Time, r = in reverse order (ascending order) $ ls -lsr ## l = show List, s = Sort by size, r = in reverse order (ascending order)
AWK:


Read data line by line and act upon the strings and patterns in each of those lines.
$ ls -l | awk '{print $9}'

dir1
file1.csv
file2.csv
dir2
$ echo "str1,str2,str3" | awk -F',' '{print $2}' str2
$ printf "str1\tstr2\tstr3" |awk -F'\t' '{print $2}' str2
SED:

SED stands for Stream EDitor
$ echo "it is sunny out" |sed s/sunny/rainy/ $ echo "it is sunny out" |sed s#sunny#rainy# $ echo "it is sunny out" |sed s@sunny@rainy@ $ echo "it is sunny out" |sed s:sunny:rainy: $ echo "it is sunny out" |sed s_sunny_rainy_ it is rainy out
** all the above commands will produce the same result s substitute command
/,#,@,:,_ Delimiter(s); there are more.
sunny Search string; could use Regex pattern.
rainy Replacement string
& Replaces the original string (see below)

$ echo "it is sunny out" |sed 's:sunny:"&":'< it is "sunny" out
$ echo "it is sunny out" |sed 's:sunny:(&):' it is (sunny) out
$ echo "it is sunny out" |sed 's:sunny:"&-&":' it is "sunny-sunny" out
$ echo "it is sunny out" |sed 's:sunny:"&-&-&":' it is "sunny-sunny-sunny" out

 
GREP:
Stands for: Global regular expression print
Function: Search for PATTERN in each FILE or standard input
(note: grep is case sensitive - this is inline with everything *nix. All *nix based shells are usually case-sensitive)
$grep ERROR {filename} ##This will print lines that has the word "ERROR" $ls -ltr |grep '.sql$' ##this will print lines that end with ".sql". The "$" symbol indicates that the preceeding character is the last character. $ $ ls -l | grep '^d' ##this will print lines that start with the letter "d". The "^" symbol indicates that the following is the first character'
drwxrwxrwx 5 schalla challanet 2017 Dec 12 07:44 dir1
drwxr-xr-x 2 schalla challanet 36 Dec 12 05:34 dir2 $ ls -l | grep -v '^d' *[2]
-rw-r--r-- 1 schalla challanet 37880 Dec 11 14:38 file2.csv
$grep ERROR -A 20 -B 30 {filename} ##This will print lines that has the word "ERROR"
additionally, it will display 20 lines preceeding (-A 20) the hit-line and 30 lines succeeding the hit-line (-B 30).
Removing Blank lines in a file:
grep . FILENAME sed -e /^$/d FILENAME awk /./ FILENAME

file ##shows character encoding details
$ file -i {{filename.ext}}
filename.ext: text/xml; charset=iso-8859-1
    
od }} print file/stdin as Octal Bytes
$ od -c filename.ext
-c option will show ASCII printable characters & backslash escapes.
VIM
/ searches the text prefixed with / command
/hello # searches for hello in the open file
:substitute command
it finds a text patten and replaces it based on one of the options
:%s/searchterm/replaceterm/g
Find & replace in all lines
:%s/searchterm/replaceterm/gc
does same a above but with confirmation for each replacement.
:s/searchterm/replaceterm/g
Find & replace in current line only; notice the absense of %, making this only applicable to one line
:s/searchterm/replaceterm/gi
same as above but with case insensitive
 
 
 

Ads by Google

Made with CityDesk