introduction:
before this we have two method to deal with text. one is to search by using grep, however we can only show the orgainal text on screan, no modify; the other is to edit using emacs, however we need to open a new window, and all behaviour will be record which means the orgainal one can be changed easly. now, we have another one named sed which can combine both advantages.
character:
- work on terminal as command, so will not need to open a new window.
- deal with file stored in your computer or online.
- it is a lightweight stream editor, so will not using too much memoy, the result will show on scream as stdout
- Sed works by performing any number of user-specified editing operations ("commands") on the input data. Sed is line-based, so the commands are performed on each line in order. And, sed writes its results to standard output (stdout); it doesn't modify any input files.
what does sed do:
- edit input stream. the orgainal source stay same, only patch match be modified.
- produce results on standard output. modifed date can show on scream or pipled.
how does sed work:
- make a copy of orginal file. and the left steps are works on this copy not the orgainal one
- input the first line into internal bufffer.
- act command on this line and print result on scream or file. if there are more than one command being act on one line, it will execute it in sequence, which means the result of former command can be picked by the next command.
- input the next line and repeat step 2 and 3 until the while file has been acted.
how to run it from command line:
- sed [-n] [-e] 'command' file(s)
- sed [-n] -f scriptfile file(s)
formating : using regular excpressions
- ^, $, ., *, ?, [], [^]
- \{m, n\} :repeat m or more than m times, but no more than n times.
- \(, \) : group operator or regin of interest, and alwoys using with \n. often \n appear after \(, \), \n mean nth of \(, \).
sed command syntax: sed [address[,address]] [!]command [arguments]
- sed can have 0, 1, or 2 address. and can be line number, symbol or regular expression in slash. no address means applying to each input line. one address meanwhile applying to any line match this address. twp comma_separated address means form one certain line to another certain line. an address followed by ! means DO NOT match this address.
- we can put more than one sed element in a command. if first command change the buffer, the will refer to this new stuff.
flag to command:
- g: global or all occurences of something
- p: print pattern space to stdout if substitution succeddful
- n: replace the nth occurence of pattern with replacement text.
- w filename: write the pattern space of line that are changed to resultsfile if substitution succcessful.
example:
- sed '/^#/d' myfile delecte all line starting with # as comment.
- sed '/on/,/5050/d' xinetd.confsed will print the text after 5050. sed -n '/on/,/5050/d' xinetd.confsed will print nothing. sed -n '/on/,/5050/p' xinetd.confsed will print out text between on and 5050. sed '/on/,/5050/p' xinetd.confsed will print the line between on and 5050 each for twice, becasue we first print current line into buffer again, then in 5050, print again.
- s/.*// means subtitute all text found for an empty string
- who sed -n ‘s/.*(\(.*\))/\1/p’ sort > machines.txt
using different delimiters:
- sed tries to match the longest expression!
- using non-'>' character: sed -e 's/<[^>]*>//g' UST.html
character classes-posix compliant sed:
- sed ‘s/[[:space:]]//g’ myfile
- how can we just select but not subtitution or other?