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

Advertisements

2 thoughts on “UNIX-1.4 Sort and Sed Commands

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s