Live data from Hacker News

Useful sed scripts and patterns

github.com

71–80 of 124 posts

Re: Useful sed scripts and patterns

#71
post #64
post #9

There are a ton of little errors in this post (at the time of my leaving this comment, of course): the variable delimiter example is missing the close _, the first word example is missing the open ', and the except line 5 example is missing a space; meanwhile, these snippets will cause bad habits, such as thinking about "words" as "things involving letters", using -r instead of -E, and a massive knowledge gap with re…

100% agree. This document shows practices of sed, but the author doesn't seem aware of best practices . A sed script starting with '#!/bin/bash' is a bad practice.

Ha, you got me there. I remembered incorrectly that sed scripts where bad practice and one should use bash instead. I've corrected the mistake.

What other good practice suggestions do you have? (if you're comfortable answering and have the time)

Re: Useful sed scripts and patterns

#72
post #20

Nice descriptions, but some of them need changes to match the command. For example: sed '1~2p' file.txt #needs -n option s '1,$ s/foo/bar/' file.txt #s --> sed and 1,$ --> 5,$ Many commands using `-r` do not need the option for the command used (for ex: `sed -r '/start/q'`). Also, using `-E` is preferred instead of `-r` since some of the other implementations support this option but not `-r`. --- I wrote a book on GN…

Thank you for the constructive feedback, I've corrected the mistakes pointed out. I've also changed -r to -E based on your (and some other people's) advice.

Re: Useful sed scripts and patterns

#73

This is awkward… I know it’s hip and cool to ask for donations, but for a tiny collection of sed scripts which aren’t including detailed explanations? I feel like sed is what deserves the donation… http://sed.sourceforge.net/sed1line.txt ? https://www.fsf.org/about/ways-to-donate/

Hey there! Thank you for the input! I'm sorry you feel that asking for donations is unwarranted. I agree wholeheartedly that FSF deserves all the love it can get. I'm not asking to get rich or take the spotlight - I'm asking so I can keep improving that guide & create more.

I didn't added explanations (although I wanted too) since this was intended as a quick tips page for sed. I think there are much better guides than I could ever make (including the info page). In a way it works better since it's more digestible.

Thank you for the link you provided - it looks awesome! I'll look into it and append the existing guide if needed (while giving credits of course)

Re: Useful sed scripts and patterns

#74
post #63

Earlier quoted context omitted.

Thank you for the feedback and the constructive criticism. Regarding the hold space I found it very confusing to use and understand. IMO it represents the 80/20 of sed (80% effort for 20% results). You're right, I'm using -r even when it's not necessary. To my defense I think it's a good habit to have since without it regex expressions are painful to write. I didn't considered that using -E it's a better choice but I…

Most examples that use '#!/bin/bash' should be rewritten to use '#!/usr/bin/sed -f'. Example "multiple replacements": #!/usr/bin/sed -f s/a/A/ s/foo/BAR/ s/hello/HELLO/ Save it as replace.sed; chmod u+rx replace.sed; ./replace.sed myfile.txt

Thanks, I've modified to use .sed scripts instead. I used bash scripts because I remembered (incorrectly it seems) that sed scripts were unsafe.

Re: Useful sed scripts and patterns

#75
post #60

Earlier quoted context omitted.

No mention of ';' to have multiple command on one line. Example: sed '10{p;q;}' myfile.txt

Thank you for your feedback! Personally I very rarely use the ; operator since it works badly with braces and commands that use files. However I've added an example using the ; operator. PS: May I humbly point out that the command you provided will actually print up to line 10 and then a duplicate line. Like I said, unexpected (even though it seems logical). :)

Yes: forgot -n

Re: Useful sed scripts and patterns

#76

Something I crave in the worst way is a sensible story for accumulating a “toolbox” of snippets and scripts and commands. They don’t need to be ready to go, but ideally: - natural language searchable - add a small description - CLI to search, examine, and copy - not a plugin - sync with a GitHub repo.

I think you could just use a text file and then let chezmoi manage the distribution via git.

Or a personal wiki, I know people that run their own wikis for just this sort of thing

Re: Useful sed scripts and patterns

#77
post #42

Mastering one tool is probably wise, but it's also good to know which tools may be better suited for some purposes. For "keep the first word of every line", I prefer awk: awk '{ print $1 }'

>For "keep the first word of every line", I prefer awk:

Me too, though the awk version throws out any leading whitespace, so it's not quite the same.

Re: Useful sed scripts and patterns

#78
"keep the first word of every line (where word is defined by alnum chars + underscores for simplicity sake)"

  sed -E s_[a-zA-Z0-9_]+.*_\1_' file.txt
You're missing an opening single quote and a set of parentheses for this to work. It feels like you should have a unit test setup for these snippets, given that there are other typos found in other comments here.

Re: Useful sed scripts and patterns

#79

Something I crave in the worst way is a sensible story for accumulating a “toolbox” of snippets and scripts and commands. They don’t need to be ready to go, but ideally: - natural language searchable - add a small description - CLI to search, examine, and copy - not a plugin - sync with a GitHub repo.

I find tldr is good for rarely used commands: https://tldr.ostera.io/sed (That's a web version but I usually access it on the cli or in my editor)

tldr is great! I personally use this faster implementation in Rust: https://github.com/dbrgn/tealdeer

And I just became aware from reading the README that there's an even faster one written in Zig.

Post reply on HN