Monday, 30 October 2017

Useful SED commands - Linux SED

    While working in Linux, SED always plays important role in text processing. Here we've given some common examples of SED usage. Hope it will be a life saving that comes in handy!!!!!
  Let us know your shortcuts/ commands in comment section below that saved your time...
Linux Commands


  • Print the number of lines in myfile.txt
sed -n '$=' myfile.txt
            -n switch tells SED not to print all lines, which is its default action.

  • Print lines 5 through 10 from myfile.txt
sed -n '5,10p' myfile.txt

  • Print all except lines 20 through end of file
sed '20,$d' myfile.txt

  • Append line numbers to the file. (= prints line number & new line)
sed '=' myfile.txt | sed 's/\n/) /'

  •  Display lines 5-7 and 10-13 from myfile.txt
sed -n -e '5,7p' -e '10,13p' myfile.txt

  • Display all lines containing 'start'
sed -n '/start/ p' myfile.txt
           
  • Add a line after match
sed '/start/ a “MATCH FOUND”' myfile.txt

  • Add a line before match
sed '/start/ i “MATCH FOUND”' myfile.txt

  • Change a matching line
sed '/start/ c “MATCH FOUND”' myfile.txt

  • Display all lines containing beginning 'start'
sed -n '/^start/ p' myfile.txt

  • To print all the lines after finding the first occurrence of the pattern.
sed -n '/^start/,$ p' myfile.txt

  • To print 2 lines after finding the first occurrence of the pattern.
sed -n '/^start/, +2 p' myfile.txt

  • Adds some text before line numbers 3 to 5
sed '3,5 s/^/#/' myfile.txt

  • To remove blank lines ( ^ denotes start of the line, $ denotes end of line)
sed '/^$/d' myfile.txt

  • To remove lines starting with 'start'
sed '/^start/d' myfile.txt

  • To remove lines ending with 'start'
sed '/start$/d' myfile.txt

  • To remove lines containing 'start' and 'end'    (.* stands for any character
sed '/.*start.*end.*/d' myfile.txt

  • To remove lines starting or ending with '#' or empty lines
sed '/^#\|#$\|$^/d' myfile.txt

  • To replace multiple blank spaces with a single space
sed '/  */ /g' myfile.txt

  • To insert one blank line every line
sed G myfile.txt

  • To insert two blank line every line
sed 'G;G' myfile.txt

  • To replace every instance of the word version with story in myfile.txt, do:
sed 's/version/story/g' myfile.txt

  • To ignore character case, use 'gi' instead of 'g'
sed 's/version/story/gi' myfile.txt

  • To replace words only within a line range (30 through 40, for example)
sed '30,40 s/version/story/g' myfile.txt

  • To replace words except within a line range (30 through 40, for example)
sed '30,40!s/version/story/g' myfile.txt

  • To replace only 2nd match of the search pattern
sed 's/version/story/2' myfile.txt

  • Emulating dos2unix with inline editing, by removing (^M)  from the file created in Windows, and save the output in same file.
sed -i 's/\r//' myfile.txt

  • Replacing words only if a separate match is found. For example replace to replace 'start' with 'stop' only if the word services is found in the same line
sed '/services/ s/start/stop/g' myfile.txt

  • Performing two or more substitutions at once in the same file
sed -i 's/that/this/gi;s/line/verse/gi' myfile.txt

  • To replace a word beginning with uppercase or lowercase with another word, example word 'zip' or 'Zip' with 'rar' in myfile.txt
sed 's/[Zz]ip/rar/g' myfile.txt

  • To replace a pattern with some text including search pattern.
sed 's/Zip/Create & and send mail/g' myfile.txt
The above command does Zip to  Create Zip and send mail to

  • Do replacement and write the changes done to a seperate file.
sed 's/Zip/Create & and send mail/w changes.txt' myfile.txt
            changes.txt will have  “Create Zip and send mail to”

  • Transform function works similar to replacement, instead it works on the character replacement in place of pattern. For example replace  h→H, s→S, i→I, x→W
     sed 'y/hsix/HSIY/' myfile.txt
     sed '2y/hsix/HSIY/' myfile.txt  => performs in 2nd line 

  • Read function, prints the contents of the specified file in the next line where the pattern is found. For example, when 'NAME:' is found, add the contents of 'namelist.txt'
 sed '/NAME:/r namelist.txt' myfile.txt


  • Switching pairs of words. Lets say, to switch  'first, last' with 'last, first' in the lines starting with 'NAME'
sed 's/^NAME: \(\w*\), \(\w*\)/NAME: \2, \1/g' myfile.txt
            or
sed 's/\(\w\+\), \(\w\+\)/\2, \1/g' myfile.txt
            or
sed 's@\(\w\+\), \(\w\+\)@\2, \1@g' myfile.txt
 
Valid delimiters can be  @ | ! ^
with 's!/dev/null!/dev/stdout!gp'  /dev/null becomes /dev/stdout
useful when the string has '/' characters.

Below table provides the short codes for pattern matching. It is very similar to Perl RegEx. 
Code
Description
\w
 Alphanumeric Characters including _
\W
 Non-Alphanumeric Characters
\s
 White Space
\S
 Non-White Space
\d
 Digits
\D
 Non-Digits
.
 Any character
*
  Zero or more occurrence
\+
  One or more occurrence
\?
  Zero or one occurrence
\{m\}
  Exactly m number of occurrence
\{m,n\}
  Within m to n occurrence
\{m,\}
  Greater or equal to  m  occurrence


Reactions:

0 comments:

Post a Comment

Search Here...