Text - Advanced

grep

grep (Global Regular Expression Print) (returns all lines which contain the matching regex pattern from file)

$ grep 'regex' [file1] ...

$ grep abcd file
-i 		case insensitive match
-c		print only count of matching lines
-n		print matched line with number
-l		print only the names of files that have lines that match
-v		print all lines that DO NOT match the pattern

-r "pattern" dir 	recursive search (for dirs)
-R					same as above, but follow symlinks
-w "is" 	   		check for full words (can't match "his", "this", "tiss" now)
-E 'regex'    		regex uses extended syntax. Ex - (this|that|those)

sed

sed (Stream Editor) (perform transformations (substitute, delete, append) on lines of text)

$ sed COMMAND-PATTERN file

$ sed -e s/unix/linux/ file 			(substitute; replace text)
$ sed -e 's/unix/linux/' -e '5d' file 	(can issue multiple commands using -e) 
$ sed '/unix/' file 					(print first line containing unix)
$ sed '/unix/ig' file 					(print all lines containing unix, case-insensitive)
$ sed 's/unix/linux/' file 				(substitute only the 1st occurance of unix with linux)
$ sed 's/unix/linux/3' file 			(substitute only the 3rd occurance of unix with linux)
$ sed 's/unix/linux/g' file 			(substitute all, global)
$ sed 's/unix/linux/3g' file 			(substitute 3rd to all occurances)
$ sed '3 s/unix/linux/' file 			(replace string only on 3rd Line)
$ sed '1,3 s/unix/linux/' file
$ sed 's/unix/{&}/g' file 				(wrap all occurances on unix in {})
$ sed 's/unix/linux/p' file 			(if replaced, then print new line twice on the console, otherwise only once)
$ sed -n 's/unix/linux/p' file 			(only print once even if replaced)
$ sed '5d' file 						(delete 5th line)
$ sed '$d' file 						(delete last line)
$ sed '3,$d' file 						(delete 3rd to last lines)
$ sed '/unix/d' file 					(delete line having pattern)

awk

awk A scripting language used to process fields of text. Feels like an earlier and much simpler relative of the C programming language.

Notes: /linux-tools/awk