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
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
 $ ls -lsr
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:
$ ls -l | grep '^d'
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

Removing Blank lines in a file:
grep . FILENAME
sed -e /^$/d FILENAME
awk /./ FILENAME
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