UNIX - Class6 - sed - Detail

1,005 views

Published on

It will provide You a Brief knowledge about sed (UNIX Tool for Non Interactive Editing)

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,005
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
50
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

UNIX - Class6 - sed - Detail

  1. 1. UNIX - sed Non-Interactive Stream Editor Presentation By Nihar R Paital
  2. 2. Introduction Developer : Lee E. McMahon Developed during: 1973-1974 Developed at : Bell Labs Category : UNIX Utility Supported by : All UNIX flavors Nihar R Paital
  3. 3. For practicing the commands please create a filetest1.txt and copy the below contents into test1.txt sssprint Telecom3sprint Telecomsprintwe are working in sprintIBM0IBMnihar,rahulk,sam,papauWe are working in IBM6We are working in IBMnihar:rahulk:sam:papaunihar,rahulk,samWe are working in IBM India Nihar R Paital
  4. 4. It performs basic text transformation on an inputstream ( A file / input from a pipeline ) Transformation using input file$ sed -e ‘s/oldval/newval/g’ inputFile > outputFileExample:$ sed -e ‘s/mumbai/pune/g’ stud.txt > stud.txt.bak Transformation using a filter in a pipeline$ generate_data | sed -e ‘s/oldval/newval/g’Example:$ cat stud.txt | sed -e ‘s/mumbai/pune/g’ > stud.txt.bak Nihar R Paital
  5. 5. Main options of sed p - print d - delete s - substitute Nihar R Paital
  6. 6. Below special characters have vital role using sed Character Description  ^ Matches the beginning of the line.  $ Matches the end of the line.  . Matches any single character.  * Will match zero or more occurrences of the previous character.  [] Matches all the characters inside the [ ]. Nihar R Paital
  7. 7. Examples (^ Matches the beginning of the line)$ sed /^sprint/p test1.txt Will Print the lines 2 times which contain sprint as a first ward of the line$ sed -n /^sprint/p test1.txt Will Print the lines which contain sprint as a first ward of the line$ sed s/^sprint/IBM INDIA/g test1.txt Will Substitute “IBM INDIA” in place of “sprint” if it appears at beginning of line.$ sed /^sprint/d test1.txt Will delete all lines containing “sprint” as a first word R Paital line. Nihar of the
  8. 8. Example ($ Matches the end of the line)$ sed /sprint$/p test1.txt Will Print the lines 2 times which contain sprint as a last ward of the line$ sed -n /sprint$/p test1.txt Will Print the lines which contain sprint as a first ward of the line$ sed s/sprint$/IBM INDIA/g test1.txt Will Substitute “IBM INDIA” in place of “sprint” if it appears at end of line.$ sed /sprint$/d test1.txt Will delete all lines containing “sprint” as a first word of the line.$ sed -n /^$/p test1.txt Will delete all blank lines Nihar R Paital
  9. 9. Example (. Matches any single character)$ sed -n /./p test1.txt It will print the lines which at least contains a single Character. It can be used for ignoring all blank lines.$ sed -n /../p test1.txt It will print the lines which at least contains a single Character.$ sed -n /IB./p test1.txt It will print the lines which at least contains a single Character after the String IB”. Nihar R Paital
  10. 10. Example (* Will match zero or more occurrences of the previous character)$ sed -n / */p test1.txt It will print all lines like “cat test1.txt”$ sed -n / */p test1.txt It will print the lines which contains at leaset 2 characters except delimited lines.$ sed -n /.*,.*,.*/p test1.txt It will print the lines where the strings are separated by at least 3 commas(,).$ sed -n /.*,.*,.*/p test1.txt | cut -d"," -f 3 It will print the 3rd filed of the lines where the strings are separated by at least 3 commas(,).$ sed -n /We*/p test1.txt Nihar R Paital It will print all the lines where at least the string “We” is present
  11. 11. Example ([ ] Matches all the characters inside the [ ])$ sed -n /[abc]/p test1.txt This will print all the lines which contains either a or b or c as part of the line.$ sed -n /^[0-9]/p test1.txt This will print all the lines which contains the digit 0 to 9 as part of line$ sed -n /^[^0-9]/p test1.tx This will print all the lines which does not contains the digit 0 to 9 as part of line$ sed s/[abc]/we/g test1.txt This will replace the string “WE” where either a or b or c is present as part of the line. Nihar R Paital
  12. 12. Sed – Line number manipulation1 first line$ last linem,n m to n lineExample:$ sed -n 3,7p test1.txtIt will print the lines from 3 to 7$ sed -n n;n;n;p test1.txt ( every 4th line )It will print every 4th line of the file Nihar R Paital
  13. 13. Nihar R Paital

×