Pages

Saturday, December 12, 2015

sed command one liners


Syntax for search and replace (s)
sed 's/SEARCH/REPLACE/OPTIONS' FILE_NAME.txt

With no option specified for search and replace (s) will have first occurrence replaced
sed 's/SEARCH/REPLACE/' FILE_NAME.txt

To search and replace (s) Xth occurrence a string
sed 's/SEARCH/REPLACE/X' FILE_NAME.txt

To search and replace (s) all occurrences a string
sed 's/SEARCH/REPLACE/g' FILE_NAME.txt

To search and replace (s) all occurrences a string and display only changed/altered lines
sed 's/SEARCH/REPLACE/gp' FILE_NAME.txt

To search and replace (s) all occurrences a string and display only changed/altered lines (to suppress display pattern buffer)
sed -n 's/SEARCH/REPLACE/gp' FILE_NAME.txt

To search and replace (s) all occurrences a string, display only changed/altered lines and write them to separate file_name (to suppress display pattern buffer)
sed 's/SEARCH/REPLACE/gpw ALTERED.txt' FILE_NAME.txt

To search and delete (s) all occurrences a string
sed 's/SEARCH/ /g' FILE_NAME.txt

To delete last two characters of all lines (s)
sed 's/..$/ /g' FILE_NAME.txt

To delete first two characters of all lines (s)
sed 's/^../ /g' FILE_NAME.txt

To search and replace (s) all occurrences a string provided an additional string1 is also found on same line
sed '/ADDITIONAL/s/SEARCH/REPLACE/g' FILE_NAME.txt

To delete (d) all lines starting with hash # and display output
sed 's/^#.*//g; /^$/d' FILE_NAME.txt

To delete Xth line and display output
sed 'X d' FILE_NAME.txt

To delete last line and display output
sed '$ d' FILE_NAME.txt

To delete Xth line and alter file_name
sed -i 'X d' FILE_NAME.txt

To delete all lines from 1st to Yth
sed '1,Y d' FILE_NAME.txt

To delete all lines from Xth to Yth
sed 'X,Y d' FILE_NAME.txt

To delete all lines from Xth to last
sed 'X,$ d' FILE_NAME.txt

To delete every Xth line starting from Yth line
sed 'X~Y d' FILE_NAME.txt

To delete a line containing a pattern
sed '/SEARCH/ d' FILE_NAME.txt

To delete a line not containing a pattern
sed '/SEARCH/ !d' FILE_NAME.txt

To delete X lines starting from the line containing a pattern (Next lines from pattern)
sed '/SEARCH/,X d' FILE_NAME.txt

To delete lines till end of file_name starting from the line containing a pattern (Next lines from pattern)
sed '/SEARCH/,$ d' FILE_NAME.txt

To delete lines starting from Xth line till line containing a pattern
sed 'X,/SEARCH/ d' FILE_NAME.txt

To delete lines from starting of file_name till line containing a pattern
sed '1,/PATTERN/ d' FILE_NAME.txt

To delete all blank lines
sed '^$ d' FILE_NAME.txt

To delete set of lines within two pattern lines
sed '/PATTERN1/,/PATTERN2/ d' FILE_NAME.txt

To delete lines containing either of two patterns
sed '/PATTERN1\|PATTERN2/ d' FILE_NAME.txt

To append a line after Xth line
sed 'X a ADDITIONAL_LINE' FILE_NAME.txt

To append a line after last line
sed '$ a ADDITIONAL_LINE' FILE_NAME.txt

To append the line after the line where pattern match is found
sed '/SEARCH/ a ADDITIONAL_LINE' FILE_NAME.txt

To insert the line before Xth line.
sed 'N i ADDITIONAL_LINE' FILE_NAME.txt

To insert the line before last line.
sed '$ i ADDITIONAL_LINE' FILE_NAME.txt

To insert the line before every line where pattern match is found.
sed '/SEARCH/ i ADDITIONAL_LINE' FILE_NAME.txt

To print Xth line
sed -n 'Np' FILE_NAME.txt

To print last line
sed -n '$p' sedtest.txt

To print the block of lines starting at line number X and ending at line number Y.
sed -n 'X,Yp' FILE_NAME.txt

To print lines starting from Xth up to last line
sed -n 'X,$p' FILE_NAME.txt

To print Xth  line and every Yth  line coming after that.
sed -n 'X~Yp' FILE_NAME.txt

To print the line that contains the pattern
sed -n '/SEARCH/p' FILE_NAME.txt

To print lines excluding a Pattern
sed -n '/SEARCH/!p' FILE_NAME.txt

To insert blank lines between all lines in a file_name
cat file_namename | sed G

To insert blank lines between all lines in a file_name which already has blank lines in it, but Output file_name should contain no more than one blank line between lines of text.
cat file_namename | sed '/^$/d;G'

To triple space a file_name
cat file_namename| sed 'G;G'

To undo double-spacing
cat file_namename | sed G | sed 'n;d'

To number each line of a file_name (simple left alignment for numbers).
sed = file_namename | sed 'N;s/\n/\t/'

To number each line of a file_name (number on left, right-aligned)
sed = file_namename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'

To number each line of file_name, but only print numbers if line is not blank
sed '/./=' file_namename | sed '/./N; s/\n/ /'

To count lines (emulates "wc -l")
sed -n '$='

To delete leading whitespace (spaces, tabs) from front of each line aligns all text flush left
cat file_namename | sed 's/^[ \t]*//'                   

To delete trailing whitespace (spaces, tabs) from end of each line
cat file_namename | sed 's/[ \t]*$//'

To delete BOTH leading and trailing whitespace from each line
cat file_name_name | sed 's/^[ \t]*//;s/[ \t]*$//'

To insert 5 blank spaces at beginning of each line (make page offset)
cat file_name_name | sed 's/^/     /'

To align all text flush right on a 79-column width
sed -e :a -e 's/^.\{1,78\}$/ &/;ta'  # set at 78 plus 1 space

To substitute "XXX" with "YYY" ONLY for lines which contain "WWW"
cat file_namename | sed '/WWW/s/XXX/YYY/g'

To substitute "XXX" with "YYY" EXCEPT for lines which contain "WWW"
cat file_namename | sed '/WWW/!s/XXX/YYY/g'

To change "XXX" or "YYY" or "ZZZ" to "AAA"
cat file_namename | sed 's/XXX/AAA/g;s/YYY/AAA/g;s/ZZZ/AAA/g' 
cat file_namename | gsed 's/XXX\|YYY\|ZZZ/AAA/g'               

To reverse order of lines
cat file_namename | sed '1!G;h;$!d'              
cat file_namename | sed -n '1!G;h;$p'            
Note: causes blank lines to be deleted

To reverse each character on the line
cat file_namename | sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'

To join two consecutive lines side-by-side by removing newline between them
cat file_namename | sed '$!N;s/\n/ /'

If a line begins with an equal sign, append it to the previous line and replace the "=" with a single space
cat file_namename | sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'

To add commas to numeric strings, changing "1234567" to "1,234,567"
gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta'                    
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' 

To add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
 gsed '0~5G'                  # GNU sed only
 sed 'n;n;n;n;G;'             # other seds

To print first 10 lines of file_name (similar to "head")
 sed 10q

To print first line of file_name (similar to “head -1")
 sed q

To print the last 10 lines of a file_name (similar to "tail")
 sed -e :a -e '$q;N;11,$D;ba'

To print the last 2 lines of a file_name (similar to "tail -2")
 sed '$!N;$!D'

To print the last line of a file_name (Similar to "tail -1")
 sed '$!d'                   
 sed -n '$p'                  

To print only lines which match regular expression (Similar to "grep")
 sed -n '/SEARCH/p'          
 sed '/SEARCH/!d'            

To print only lines which do NOT match pattern or regex (Similar to "grep -v")
 sed -n '/SEARCH/!p'         
 sed '/SEARCH/d'             

To print the line immediately before a pattern or regex, but not the line containing the pattern or regex
 sed -n '/SEARCH/{g;1!p;};h'

To print the line immediately after a pattern or regex, but not the line containing the pattern or regex
 sed -n '/SEARCH/{n;p;}'

To print 1 line of context before and after pattern or regex, with line number indicating where the pattern or regex occurred (similar to "grep -A1 -B1")
 sed -n -e '/SEARCH/{=;x;1!p;g;$!N;p;D;}' -e h

To grep for AAA and BBB and CCC (in any order)
 sed '/AAA/!d; /BBB/!d; /CCC/!d'

To grep for AAA and BBB and CCC (in that order)
 sed '/AAA.*BBB.*CCC/!d'

To grep for AAA or BBB or CCC (emulates "egrep")
 sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d   
 gsed '/AAA\|BBB\|CCC/!d'                       

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

To print paragraph if it contains AAA and BBB and CCC (in any order)
 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'

To print paragraph if it contains AAA or BBB or CCC
 sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
 gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d'        

To print only lines of X number of characters or longer
 sed -n '/^.\{X\}/p'

To print only lines of less than Y number of characters
 sed -n '/^.\{Y\}/!p'        , corresponds to above
 sed '/^.\{Y\}/d'            , simpler syntax

To print all of file_name EXCEPT section between 2 regular expressions
 sed '/PATTERN1/,/PATTERN2/d'

To delete duplicate, consecutive lines from a file_name (emulates "uniq"). i.e. First line in a set of duplicate lines is kept, rest are deleted.
 sed '$!N; /^\(.*\)\n\1$/!P; D'

To delete duplicate, nonconsecutive lines from a file_name. Beware not to overflow the buffer size of the hold space, or else use GNU sed.
 sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'

To delete ALL blank lines from a file_name (Similar to "grep '.' ")
 sed '/./!d'                           


References :

Back To Top

No comments:

Post a Comment