SED
Find and Replace:
Sed is often used as a find-and-replace tool.
sed 's/Glenn/Harold/g' oldfile >newfile
will replace every occurrence of "Glenn" with the word "Harold", wherever it occurs in the file. The "find" portion is a regular expression ("RE"), which can be a simple word or may contain special characters to allow greater flexibility (for example, to prevent "Glenn" from also matching "Glennon").
SED Leaving Space:
My very first use of sed was to add 8 spaces to the left side of a file, so when I printed it, the printing wouldn't begin at the absolute left edge of a piece of paper.
sed 's/^/ /' myfile >newfile # my first sed script
sed 's/^/ /' myfile | lp # my next sed script
SED display one paragraph of a file:
sed could display only one paragraph of a file, beginning at the phrase "and where it came" and ending at the phrase "for all people". My script looked like this:
sed -n '/and where it came/,/for all people/p' myfile
SED Display 12 to 18 lines:
found that sed could show me only (say) lines 12-18 of a file and not show me the rest. This was very handy when I needed to review only part of a long file and I didn't want to alter it.
# the 'p' stands for print
sed -n 12,18p myfile
SED Display everything but of those 12, 18:
sed could show me everything else BUT those particular lines, without physically changing the file on the disk:
# the 'd' stands for delete
sed 12,18d myfile
SED double-space my single-spaced file when it came time to print it:
double-space my single-spaced file when it came time to print it:
sed G myfile >newfile
No comments:
Post a Comment