Sed - An Introduction and Tutorial
Popularity Report
![]() |
|||
![]() |
|||
![]() |
|||
![]() |
|||
![]() |
|||
![]() |
URL Tag Cloud
Bookmark History
Saved by 123 people (-25 private), first by anonymouse user on 2006-03-02
Public Sticky notes
Highlighted by david_r_whiting
Highlighted by pklausner
Highlighted by calamityfactors
Highlighted by hamming229
Highlighted by hamming229
Highlighted by yw2298
Printing with p
Another useful command is the print command: "p." If sed wasn't started with an "-n" option, the "p" command will duplicate the input. The command
- sed 'p'
will duplicate every line. If you wanted to double every empty line, use:
- sed '/^$/ p'
Adding the "-n" option turns off printing unless you request it. Another way of duplicating head's functionality is to print only the lines you want. This example prints the first 10 lines:
- sed -n '1,10 p' <file
Sed can act like grep by combining the print operator to function on all lines that match a regular expression:
- sed -n '/match/ p'
Highlighted by aowongster
The "=" command prints the current line number to standard output. One way to find out the line numbers that contain a pattern is to use:
- # add line numbers first,
# then use grep,
# then just print the number
cat -n file | grep 'PATTERN' | awk '{print $1}'
The sed solution is:
- sed -n '/PATTERN/ =' file
Highlighted by david_r_whiting


Public Comment
on 2006-08-23 by inetgate
on 2006-11-03 by pistos