Thursday 17 November 2016

SED cheat sheet


This article is exactly what the title says. It's a cheat sheet. IF you are looking for information how to learn to use sed with detailed explanation of the usage examples then this article is not the best avenue. However, if you are looking for a lot of usage examples with brief descriptions then this is the right place.



SED ExpressionDescription
sed 's/sahil/{&&&}/' somedata.txt [put the matched string in parenthesis & replicate it as many times as & is specified]
sed '2,3 s/sahiltech/Admin/' somedata.txt [replace string on 2nd & 3rd line. If there is a $ after , then replace till last line]
sed '/linux/ s/unix/Admin/' somedata.txt [do replace on lines matching a pattern]
sed -n '5,10p' somedata.txt [print lines 5 to 10 only]
sed '20,35d' somedata.txt [print the file omitting lines 20 to 35]
sed -n -e '5,7p' -e '10,13p' somedata.txt [print non consecutive line ranges]
sed 's/version/story/gi' somedata.txt [case insensitive search & replace]
ip route show | sed 's/  */ /g' [replace multiple blank spaces using a single space]
sed '30,40 s/version/story/g' somedata.txt [search & replace within a given range of lines]
sed 's/[Zz]ip/rar/g' somedata.txt [using regex in search & replace]
sed -n '/^Jul 1/ p' /var/log/secure [view lines containing a given pattern]
sed -e '/^#/d' -e '/^$/d' somedata.txt [remove comments & blank lines]
sed 'G;G' somedata.txt [inset 2 blank lines between each lines]
sed '0~2 a\\' somedata.txt | tr -d '\\' [insert a blank line after every 2 lines]
sed -i'.orig' 's/this/that/gi' somedata.txt [take backup of orignal file as filename.orig & perform replace on original file]
sed -i 's/old/new/g' somedata.txt [modify original file]
sed -ibak 's/old/new/g' somedata.txt [backup & modify original file]
sed -n 's/old/new/gpw output' somedata.txt [write changes to new file called output]
sed 's/\-.*//g' somedata.txt [empty everything after the - charector]
sed 's/...$//' somedata.txt [delete last 3 charectors from each line]
sed -e 's/#.*//' somedata.txt [eliminate comments from a file]
sed -e 's/#.*//;/^$/d' somedata.txt [eliminate comments & empty lines from a lines, -e to execute multiple sed commands separated by ;]
sed -e '1w out' -e ' 2w out' -e ' $w out' somedata.txt [wwrite 1st, 2nd & last line to new file called out]
sed '1,5w out' somedata.txt [write lines 1 to 5 to a file out]
sed -n -e '/Storage/w output.txt' somedata.txt [write lines with pattern Storage to a new file]
sed -n '/Storage/,+2w output.txt' somedata.txt [write lines matching pattern plus next two lines to a new file]
sed '1,3 s/^/hello: \t/g' somedata.txt [add hello to the start of 1st 3 lines]
sed -e '4d' -e '2d' somedata.txt [delete 4th & 2nd line from file]
sed -n -e '/Software/p' -e '/Storage/p' somedata.txt [search & print matching pattern only]
sed -e '1d' -e '$d' -e '/^$/d' somedata.txt [delete 1st, last & all blank lines]
sed 's/\([^:]*\).*/\1/' /etc/passwd [extract usernames from password file]
sed '3 a Cool gadgets and websites' somedata.txt[append a line after the 3rd line of file]
sed '/vsun001*/a still testing sed' somedata.txt [insert line after every line with matching patter]
sed '2 i this is sed insert test' somedata.txt [insert a line before 2nd line of file]
sed '/vsun001*/i insert testing' somedata.txt [insert a line before every line matching the pattern]
sed '/this is/c is this a test' somedata.txt [replace lines mathing pattern 'this is' with line 'is this a test']
sed '2c not found' somedata.txt [replace 2nd line in file somedata.txt with the line not found]
sed '=' somedata.txt [print line number above line being printed]
sed -n '/vsun0012/=' somedata.txt [print line number containing the pattern vsun0012]
sed 's/0\{3\}//g' somedata.txt [remove 3 zeros from numbers in the file. For example, 7.000 will become 7.]
sed '0~3 s/$/\nMy Text/g' somedata.txt [insert the line My Text after every 3 lines. In 0~3, 0 is the first addition & 3 denotes after how many lines the subsequent text is to be inserted]
sed 's/.\{2\}/&#/g' somedata.txt [insert a # character after every 2 characters in the file]
sed 'N;s/\n/ /g'[join 2 line. replace newline with a space]
sed 'N;N;s/\n/ /g'[join 3 consecutive lines]
sed -e '{N;s/\n/@/}' somedata.txt[join 2 consecutive lines & place a @ symbol b/w the lines]
sed 's/sahil/Linux Admin/2g' somedata.txt [replace from 2nd occurnace till end of line in the file]

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...