UNIX-1.5 Awk, Cut, Last, Wc Commands Video Tutorial


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

awk -F’|’ ‘BEGIN{ initialization } { code block } END{ finalization }’ – general syntax
default delimiter is space ‘ ‘
VAR=”value” – define a variable
print VAR – print a variable
NF is number of fields
$NF is value of last field
print “‘$EXTERNAL_VAR'” – double single $EXTERNAL_VAR single double
VAR~/pattern/ – pattern patching
VAR!~/pattern/ – does not match
FS returns value of field separator, same as -F
OFS returns output field separator
print $1,$2 is separated
NR returns line number
FILENAME returns current filename
awk -v var1=value1 – pre-assign values to variables in awk
awk -F, ‘conditions { code block }’ filename – condition must be right before code block

cut – get columns from file
cut -d, -f1-2 filename – print 1st to 2nd columns
cut -c1-5 filename – print first 5 characters
cut -d, -f2- filename – print from 2nd column to last column
cut -d, -f1,4,5 filename – print 1st, 4th and 5th column
cut -d, -f-3 filename – print from 1st to 3rd column
cut -d, -f1,3-5 filename – print 1st, 3rd to 5th column

wc – word count
wc -l – line count
wc -c – print number of bytes
wc – print number of newline, word and bytes
wc -m – character count
wc -w – word count

last – check login history
last -10 – show last 10 logins
last -F – show full login information

Advertisements

UNIX-1.4 Sort and Sed Commands


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

sort – sort file
sort -t, -k1 file1 – sort file1 by column 1, delimited by comma (,)
sort -r -t, -k1 file1 – in reverse
sort -n -t, -k1 file1 – sort by numerical value
sort -f file1 – case insensitive sort
sort -t’|’ +0 -2 – sort by the first 2 fields, starting from 0+1th field and end at 2nd field.

sed – special editor
sed ‘s/pattern/replacement/gi’ – search and replacement globally and case insensitively
sed ‘s/pattern/&replacement/gi’ – & represents the pattern found
sed ‘s/\(group\)pattern/\1/gi’ – \1 contains value of 1st group
sed ‘s/hello/bye/2g’ – replacements second occurrence of hello
sed ‘/pattern/p’ – will print out occurrences of pattern
sed -e ‘command1’ -e ‘command2’ – run multiple commands
sed ‘s/'”$VAR”‘/replacement/’ – using external variables in sed
sed ‘2 s/pattern/replacement/’ – replace only for line 2. you can also specify line range, 1,10
sed ‘/pattern1/ s/pattern2/replacement/’ – find pattern1, for those lines, replace pattern2 with replacement
sed ‘/start/,/end/ s/pattern/replacement/’ – using patterns as line number range. you can also mix line number with pattern
sed ‘1,10d’ – deletes line 1 to 10. you can also mix pattern range as well
sed ‘100,$d’ – delete from line 100 to end of file
sed ‘100,$!d’ – delete from line 1 to line 100. ! means inverse operation.
sed ‘y/ABC/abc/’ – replacement each character with corresponding characters
http://www.grymoire.com/Unix/Sed.html

regular expression
[0-9a-z]* – match 0 or more alphanumeric characters
[0-9a-z]+ – match 1 or more alphanumeric characters
[^a-z] – any character does not contain a-z

UNIX-1.3 UNIX Commands and Options


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

du – data usage
du -sh – find how much disk space used in current directory

df – data file system
df -h – find disk space usage

cp – copies files
cp file1 file2 – copy file1 to file2
cp file1 dir/file2 – copy file1 to file2 in dir
cp file1 dir/ – copy file1 to dir with same filename
cp -rf dir1 dir2 – recursively copy dir1 to dir2
cp -i file1 file2 – prompt before overwriting file2
cp file1 file2 file3 dir1 – copy multiple files to a directory
cp -p file1 file2 – by default, permission is not preserved, use -p to do that
cp -s file1 link1 – create soft link to file1

mv – change/move files
mv file1 file2 – change filename from file1 to file2
mv -i file1 file2 – prompt before overwriting
mv file1 dir1 – move file1 to dir1
mv dir1 dir2 – move dir1 to dir2 if dir2 exists, else change name from dir1 to dir2

head – display lines in file from beginning
head file1 – display first 10 lines of file1
head -100 file1 – display first 100 lines of file1

tail – display lines in file from end
tail file1 – display last 10 lines of file1
tail -100 file1 – display last 100 lines of file1

rm – remove file or directory
rm file1 – remove file1
rm -rf dir1 – remove entire directory, never prompt
rm -i dir1 – prompt before removing

mkdir – create directory
mkdir dir1 – create dir1
mkdir -m777 dir1 – create dir1 with specified permission
mkdir -p dir1 – ignore error if dir1 already exists

tar – archive file
tar zcvf tarfilename.tar file1 file2 file3 – archive all these files into one tar ball, compressed
tar xvf tarfilename.tar – extract files from a tar ball
tar rf tarfilename.tar extra.txt – append extra.txt to existing tarfilename.tar (tarfilename.tar is not compressed)
tar tvf tarfilename.tar – list out files in tar ball

gzip – compress file
gzip file1 – compress file1, new filename will have .gz extension
gzip -d file1.gz – decompress file1.gz

chmod – change permission
chmod 777 file1 – change permission for file1 (rwxrwxrwx)
chmod +x file1 – give executable permission for all to file1 (+r, +w)
chmod u+x file1 – give executable permission for owner to file1 (g+x, o+x)
chmod -R 777 dir1 – change permission for all contents in dir1