onlyforbopi
2/3/2017 - 11:29 AM

SED SIMPLE FUNCTIONS

SED SIMPLE FUNCTIONS

Substitute only 1st instance / line				
sed 's/foo/bar/'					

#
Substitute only Xth instance / line				
sed 's/foo/bar/4'					

#
Substitute all instances in a line				
sed 's/foo/bar/g'					

#
Replace the next-last-cast				
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/'					

#
Replace only the last case				
sed 's/\(.*\)foo/\1bar/'					

#
Substitute only on lines that match pattern				
sed '/baz/s/foo/bar/g'					

#
Substitute only on lines that don’t match pattern				
sed '/baz/!s/foo/bar/g'					

#
Change pattern or pattern or pattern to Y				
sed 's/pattern1/red/g;s/pattern2/red/g;s/pattern3/red/g'					

#
Change multiple patterns to Y (same as above, GNU sed)				
gsed 's/pattern1\|pattern2\|pattern3/Y/g'					
Double Space a File				             
sed G					

#
Double Space (some blank lines)        
sed '/^$/d;G'					

#
Triple Space a File				             
sed 'G;G'					

#
Undo Double Spacing				             
sed 'n;d'					
Print first X lines of a file ( Emulates Head)				sed Xq (ie sed 10q)					
print first line of a file (Emulates Head -1)				sed q					
Print the last ten lines of a file (Emulates tail)				sed -e :a -e '$q;N;11,$D;ba'					
Print the last two lines of a file (Emulates tail -2)				sed '$!N;$!D'					
Print the last line of a file (Emulates tail -1)				sed '$!d'     or    sed -n '$p'					
Print lines between two line numbers				sed -n '10,20p'					
Print two lines between two line numbers ( 2nd way )				 sed '8,12!d'					
Print a specific line from a file (ie the fifth)				sed -n 5p					
Print only lines that match pattern/regex				sed -n 'regexp/p'    OR    sed 'regexp/!d'					
Print only lines that DON’T match pattern/regex				
sed -n '/regexp/!p'   OR   sed '/regexp//d'					

#
Print line immediately above line that contains pattern				 
sed -n '/regexp/{g;1!p;};h'					

#
Print line immediately after line that contains pattern				
sed -n '/regexp/{n;p;}'					

#
Print one line before and after with numbering (emulates grep -A1 -B1)				 
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h					

#
Print lines that contain pattern 1 and 2 and 3 in any order (emulates grep)				 
sed '/AAA/!d; /BBB/!d; /CCC/!d'					

#
Print lines that contain pattern 1 and 2 and 3 in that order (emulates grep)				 
sed '/AAA.*BBB.*CCC/!d'					

#
Print lines that contain pattern 1 or 2 or 3 (emulates egrep) 1				 
sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d					

#
Print lines that contain pattern 1 or 2 or 3 (emulates egrep) GNU				 
gsed '/AAA\|BBB\|CCC/!d'					

#
Print paragraph if it contains pattern (blank lines separate paragraphs)				
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;					

#
Print paragraphs if it contains pattern 1 and 2 and 3 (any order)				 
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'					

#
Print paragraphs if it contains pattern 1 or 2 or 3 (any order)				
sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d					

#
Print only lines of 65 characters and longer				
sed -n '/^.\{65\}/p'					

#
Print only lines of 65 characters and lesser				
sed -n '/^.\{65\}/!p'					

#
Print section of file from pattern to EOF				
sed -n '/regexp/,$p'					

#
Print every line starting from line 3 and every 7 lines				
gsed -n '3~7p' 					

#
Print every line starting from line 3 and every 7 lines (all sed)				
sed -n '3,${p;n;n;n;n;n;n;}'					

#
Print section of file between two pattern/regexp (inclusive)				
sed -n '/Iowa/,/Montana/p'					
add a leading angle bracket and space to each line (quote a message)				
sed 's/^/> /'					

#
delete leading angle bracket & space from each line (unquote a message)				
sed 's/^> //'					
print all of file EXCEPT section between 2 regular expressions				
sed '/Iowa/,/Montana/d'					

#
delete duplicate, consecutive lines from a file (emulates "uniq").				
sed '$!N; /^\(.*\)\n\1$/!P; D'					

#
delete duplicate, nonconsecutive lines from a file				
sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'					

#
delete the first 10 lines of a file				
sed '1,10d'					

#
delete the last line of a file				
sed '$d'					

#
delete the last 2 lines of a file				
sed 'N;$!P;$!D;$d'					

#
delete the last 10 lines of a file				 
sed -e :a -e '$d;N;2,10ba' -e 'P;D'					

#
delete the last 10 lines of a file				 
sed -n -e :a -e '1,10!{P;N;D;};N;ba'					

#
delete every 8th line				  
gsed '0~8d'     OR      sed 'n;n;n;n;n;n;n;d;'   					

#
delete ALL blank lines from a file (same as "grep '.' ")				 
sed '/^$/d' 					

#
delete ALL blank lines from a file (same as "grep '.' ")				
sed '/./!d' 					

#
delete all CONSECUTIVE blank lines from file except the first; also				
sed '/./,/^$/!d'          # method 1, allows 0 blanks at top, 1 at EOF					

#
deletes all blank lines from top and end of file (emulates "cat -s")				
sed '/^$/N;/\n$/D'        
# method 2, allows 1 blank at top, 0 at EOF					

#
delete all CONSECUTIVE blank lines from file except the first 2:				
sed '/^$/N;/\n$/N;//D'					

#
delete all leading blank lines at top of file				
sed '/./,$!d'					

#
delete all trailing blank lines at end of file				 
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'					

#
delete all trailing blank lines at end of file				
sed -e :a -e '/^\n*$/N;/\n$/ba' 					

#
delete the last line of each paragraph				 
sed -n '/^$/{p;h;};/./{x;/./p;}'					
#
Number each line (Left)				              
sed = filename | sed 'N;s/\n/\t/'					

#
Number each line (Left, Right align)				s
sed = filename | sed 'N; s/^/      /; s/ *\(.\{6,\}\)\n/\1  /'					

#
Number each line if not blank				        
sed '/./=' filename | sed '/./N; s/\n/  /'					

#
Count lines				                          
sed -n '$='					
Align all text flush right on 70 column				sed -e :a -e 's/^.\{1,69\}$/ &/;ta' 					
Align text in the middle of 70 col (1)									
Align text in the middle of 70 col (1)									
Add space at beginning				
sed 's/^/          /'					

#
Add space at end of line				
sed 's/$/         /'					

#
Add space after pattern				
sed 's/pattern*/patterntext/'					
Append Line after Match				
sed '/\[option\]/a Hello World' input					

#
Append Line Before Match				
sed '/\[option\]/i Hello World' input					

#
Add line after X lines (ie 5)									
Delete leading whitespace (or tab)				
sed 's/[ \t]*//'					

#
Delete trailing whitespace (or tab)				
sed 's/[ \t]*$//'					

#
Delete both leading and trailing space				
sed 's/^/[ \t]*//;s/[ \t]*$//'					
Convert CR/LF TO Unix 				
sed 's/.$//'					

#
Convert LF to Dos Format				
sed 's/$'"/`echo \\\r`/"					

#
Convert LF to Dos Format (D0S -1)				
SED "S/$//"					

#
Convert LF to Dos Format (D0S -2)				
SED --n p					

#
Convert CR/LF Tto Unix Format				
tr -d \r <infile >outfile					

#
Convert CR/LF TO Unix 				
sed 'S/^m$//'					

#
Convert CR/LF TO Unix 				
sed 's/\x0D$//'					
1.  sed_spacing                : File spacing
2.  sed_count                  : File count and numbering
3.  sed_convertcrlf            : File converting line endings
4.  sed_del_white              : File delete whitespace
5.  sed_add_white              : File add whitespace
6.  sed_align                  : File text aligning left right justify
7.  sed_add_line               : File add lines at specific points
8.  sed_substitute             : File text substitute
9.  sed_selective_print        : File selective printing of lines
10. sed_line_delete            : File selective deletion of lines
11. sed_quoting                : File, add quotes to file