11 ms·
Sed – An Introduction and Tutorial
- misiti3780 12y agowas interested in reading this until i saw the yellow background and couldnt stomach it - looks like a lot of great information just displayed in a horrible way
- alganet 12y agoI had the same opinion about the official sed page[1], until I noticed the footnote. It got my attention and I decided to learn. One of the best, portable, elegant tools I use every day. [1] http://sed.sourceforge.net/ http://sed.sourceforge.net/
- LukeShu 12y agoThat's not official. It's a centralized homepage for several different sed implementations, not really affiliated with any of them.
- mdaniel 12y agoIf it's available on your platform, you could print it to a local file since the print version doesn't use the same CSS as the web (OS X = print to PDF, Win = print to XPS, etc). Or, presuming you're on a modern browser and care that much about the content, you can just inspect the dom, find that <link type="text/css"...> in the head, and delete it.
- nilved 12y ago> If it's available on your platform, you could print it to a local file since the print version doesn't use the same CSS as the web (OS X = print to PDF, Win = print to XPS, etc). Yeah or you could go read a better book
- gh02t 12y agoYou'll have a hard time finding one though. The O'Reilly books are the only ones that I know of that might qualify as "better." The site is ugly, but it's one of the best references for sed.
- baby 12y agoOn firefox: View > Page Style > No Style
- tthayer 12y agoI use the Clearly browser plugin for pages like this. Works like a charm.
- a3n 12y agoIn Firefox: View/Page Style/No Style It also looks pretty good in the lynx browser, and probably other text browsers. https://en.wikipedia.org/wiki/Lynx_%28web_browser%29 https://en.wikipedia.org/wiki/Lynx_%28web_browser%29
- msoad 12y agoIf you're using Chrome, you can install Clearify[1] and read it with better readable CSS [1] https://chrome.google.com/webstore/detail/clearly/iooicodkiihhpojmmeghjclgihfjdjhj?hl=en https://chrome.google.com/webstore/detail/clearly/iooicodkii...
- LukeShu 12y agoIt's weird, it didn't always look like that. I'd been recommending it for years while it was a very Spartan HTML page with no CSS or inter-page navigation. The horrid new look came early last year.
- bewuethr 12y agoUse Instapaper?
- chrisan 12y agoThe site is so garish I actually love it for some reason.
- senorsmile 12y agoprobably the best reference besides the o'reilly books.
- JeremyMorgan 12y agoSed is one of those tools that once you learn it, you'll start to wonder how you ever got by without it. This is a great set of tutorials, he also wrote one about Awk: http://www.grymoire.com/Unix/Awk.html http://www.grymoire.com/Unix/Awk.html Get to know these two tools and you'll be amazed at the hours you can save and what you can do, especially with text files.
- Fede_V 12y agoI am always amazed by how much people can get done using piped unix commands - but, personally, I find it much easier to just use sed when I need to quickly edit a few files, and if I need to do anything slightly more elaborate, to do it in Python with a script. It's obviously much slower - but I've never been in a position where I needed insane speed to quickly fix a bunch of files.
- uxcn 12y agoI'll sometimes use sed/awk/cut/etc... to get one off summary info out of large files. Excel/LibreOffice would choke, and loading it into a database for one off is painful.
- csandreasen 12y agoI fully agree. I find it's worth keeping large files like these in a tab delimited format instead of CSV specifically so that I can run them through the standard Unix tools instead of mucking around with quoting issues and stray commas.
- sbilstein 12y agoThe nice thing about these tools is you can count on them being on any unix machine w/o worry about installing them on a remote locked down box of some kind. You can also avoid any dependency complexity. They can be real awkward to remember however and the man pages can sometimes be a handful, so I try to store common idioms in gists or in my .bash_profile whenever I develop one.
- c3RlcGhlbnI_ 12y agoTo be honest I don't know how much he has improved on the manual. It is such a small language that you could easily read up to the the examples very quickly even if you aren't particularly interested in learning. I would suggest just giving it a look directly at https://www.gnu.org/software/sed/manual/sed.html https://www.gnu.org/software/sed/manual/sed.html Though be forewarned, something that neither document explains well is the actual syntax. As in how addresses and expressions can be used and how to read a script. The syntax is relatively simple to understand looking at some examples, but the lack of clear delimiters between the address, command, and command parameters can confuse beginners.
- josteink 12y agoBut that only covers the GNU bits. If you stick (too closely) to that one, sometimes you might use features only found in GNU Sed and think you're writing portable scripts. I think this tutorial helps clarify what is and isn't portable.
- c3RlcGhlbnI_ 12y agoWell, it does mention explicitly which parts are GNU extensions. But I see what you mean. I can write sed scripts fairly well at this point but clearly didn't internalize any of the notes about what is an extension, thus it is frustrating trying to get the non GNU versions to do anything at all.
- narrator 12y agoI just use | perl -pi -e 's/foo/bar/g' , etc for this kind of stuff. Is there anything I can't do with perl on a line that sed will do? I can see how perl is a lot more complex than sed, but I went through the whole perl learning curve back in the late 90's so it doesn't bother me that much..
- dogweather 12y agoFor me, this is the whole point of perl: You can learn one tool which replaces three, each with their own syntax: Sed, Awk, and Grep.
- cogburnd02 12y agoWait, why not just use awk? It can do pretty much everything the other two do (and can, like sed, be used to replace other things (like 'head')) and runs faster than perl at some things. (https://news.ycombinator.com/item?id=8858342 https://news.ycombinator.com/item?id=8858342)
- dogweather 12y agoI learned awk in school, and like it. But I like to choose tools which increase programmer-efficiency before machine-efficiency. Perl is just one tool and one easy syntax to learn. Its regex language is now the standard as well. Only if runtime efficiency turns out to be too slow would I re-examine choice of tools. This is like avoiding premature optimization. (I've never found perl to be too slow in practice, btw.)
- sbi 12y agoProbably not, but in some cases sed and AWK might be faster. I am a big fan of AWK. It is limited in some ways that make it impractical to use for really serious programs, but it is very expressive. Look at [1, 2]. [1]: http://c2.com/doc/expense/ http://c2.com/doc/expense/ [2]: http://www.pement.org/awk/awk1line.txt http://www.pement.org/awk/awk1line.txt
- 12y ago
- nkangoh 12y agoA friend of mine has been trying to get me to learn awk, sed, perl or grep. Honestly I only have the patience for one at the moment, which do you think is the best (taking the ease of learning into account)?
- jibberia 12y agoLearn grep. If you're familiar with using a shell, it'll be the most immediately useful. # search current and all child directories for files containing "bananas", case insensitive $ grep -ir "bananas" .
- scarecrowbob 12y agohere's an example of the first useful grep I learned : history | grep "git push"
- jpwgarrison 12y agoI alias "history | grep" to "hrep" and I'd bet it is one of my top 3-5 commands.
- spdustin 12y agoWhy not find out: history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
- scarecrowbob 12y ago:D HA! Nice! 2936 git 1166 cd 409 ll 343 ssh 302 l 279 vagrant 223 ls 201 la 151 cat 140 mkdir
- jakupovic 12y agouniq seems a bit easier to understand here: history | awk '{print $2}' | sort | uniq -c | sort -rn
- 12y ago
- jakeogh 12y agoIs there a command to automatically escape a string for use in sed? I got frusturated escaping for simple replacement: https://github.com/jakeogh/replace-text https://github.com/jakeogh/replace-text
- klshxsh 12y agoIf you find yourself having to escape a lot of slashes, then you can do something like this: echo "/foo/bar" | sed -e 's|/foo|/tmp|' (The article mentions this)
- JetSpiegel 12y agoIt can be any character really. I like sed 's,foo,bar,' sed 's@foo@bar@'
- Tobu 12y agoPython has re.escape; awk can take variable definitions from the command line: echo b |awk '/b/{print a}' a=4
- la6470 12y agoAwesome
- sidcool 12y agoI tried posting this to /r/programming. It said already submitted 8 years ago. It's a very good tutorial and I was oblivious of its existence for so long.
- michaelsbradley 12y agoThere's also an excellent (non-free) book: Definitive Guide to sed http://www.sed-book.com/ http://www.sed-book.com/ I found it to be well worth the money, though I wish it were available as a PDF.
- mbubb 12y agoFor context of 'why sed and why not x?' This was written in 1984 (I think) and still works with a few syntax adjustments. I think it is not bad discipline to return to these tools from time to time and remember core UNIX principles. http://web.stanford.edu/class/cs124/kwc-unix-for-poets.pdf http://web.stanford.edu/class/cs124/kwc-unix-for-poets.pdf I am not so sure anything that I currently am writing would/ could be relevant in 30 years. Very humbling.
- Jacky800 12y agoI have always used grymoire.com for regex and sed tutorials since probably around 2009. Thanks grymoire
- stygiansonic 12y agoGreat reference; with the awk reference on that site, this formed a great resource for learning text manipulation/searching in a Unix environment.
- sea6ear 12y agoPeter Krumins also has decent a walkthrough of sed that essentially goes through and explains it via detailed explanations of sed one-lines (The explanations are original, but the list of one-liners was already popular on the internet). Book: http://www.catonmat.net/blog/sed-book/ http://www.catonmat.net/blog/sed-book/ Free online articles: http://www.catonmat.net/blog/sed-one-liners-explained-part-one/ http://www.catonmat.net/blog/sed-one-liners-explained-part-o...