TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Go Replace - simple and fast search and replace tool for command line

70 pointsby piranhaalmost 12 years ago

10 comments

JoeAcchinoalmost 12 years ago
<p><pre><code> find ~/my/docs/ -type f -name '*.txt' -exec sed -i.bak 's/inheritance/composition/g' {} + </code></pre> This will search all files ending in `.txt` and will exec `sed 's/inheritance/composition/g'` on it. Sed modifies files in-place and saves a backup file with .bak extension (-i.bak) and is called the minimum necessary times (-exec +).<p>My best advice to someone using the command line is to learn `find` + `xargs` or, even better, `find` + `parallel`.
评论 #5749946 未加载
artagnonalmost 12 years ago
Okay, so what I learnt from this:<p>1. You've imported droundy/goopt, wsxiaoys/terminal really easily. The first provides option-parsing, and the second provides helpers for emitting ANSI escape codes.<p>2. Practically everything is overloaded. You can == to strcmp(), + to concatenate strings.<p>3. path/filepath gives you some nice goodies for path manipulation. But you have to deal with the fallouts of making naive assumptions like whether or not to recurse symlinks (why isn't this a flag in filepath.Walk?). This has resulted in ugliness in your walkFunc callback.<p>4. You have been able to abstract quite a bit without using Java-like factories, although your inconsistent error passing has me somewhat confused.<p>5. Since it's so strongly typed and there are no raw pointers, it's not going to be hard to debug at all.<p>Overall, I'd say Go is an effective language. But not beautiful, or novel in the slightest. It resembles Algol-68 (yes, a 1968 language) quite strongly, and I don't think it will drive compiler technology. Their syntax is specifically engineered to be parseable quickly (and hence super-fast AOT compilation): what the final program looks like is secondary concern. I'm not sure I see the appeal.<p>[edit: clarified that the note on compiler technology was an opinion]
评论 #5750107 未加载
评论 #5750256 未加载
评论 #5750105 未加载
评论 #5750176 未加载
lazyjonesalmost 12 years ago
Nice! svn support would be useful (ignore .svn directories) and the -r option is easy to remember, but perhaps not so much in line with the already messy standard Unix options (-r or -R for recursive operation).<p>Note that specifically for Go source code, there is "go fix -r" for syntax/context-aware code replacement (see "godoc fix").
评论 #5750013 未加载
评论 #5750127 未加载
porkeralmost 12 years ago
I have to break out the manual every time I use sed. Or awk. If this works reliably without any edge cases, it'll be fab!
评论 #5749817 未加载
madispalmost 12 years ago
For x86 binaries:<p>go get github.com/piranha/goreplace<p>go install github.com/piranha/goreplace<p>is another way to get it if you have GO and GOPATH set up.<p>I personally aliased it to 'gor' in my zshrc as the git plugin already occupied the 'gr' (short for git remote).
评论 #5749930 未加载
va1en0kalmost 12 years ago
I really like Facebook's codemod.py for stuff like that: <a href="https://github.com/facebook/codemod" rel="nofollow">https://github.com/facebook/codemod</a><p>It has something like automatic and manual modes, so you can check (and fix) every diff during the replacement
评论 #5749939 未加载
评论 #5750037 未加载
swahalmost 12 years ago
This isn't working for me. I get:<p><pre><code> C:\Users\swah\docs&#62;gr TEST panic: Given path should be anchored at / goroutine 1 [running]: main.NewIgnorer(0xc08005bcf0, 0x29, 0x0, 0x0, 0x140110, ...) /Users/piranha/dev/go/src/goreplace/ignore.go:32 +0xa4 main.main() /Users/piranha/dev/go/src/goreplace/goreplace.go:62 +0x1b7 goroutine 2 [runnable]: </code></pre> [edit] New version is working!
评论 #5750060 未加载
评论 #5750089 未加载
M4v3Ralmost 12 years ago
Wow, it's super fast AND the output looks great. Good job, piranha! Copied to /usr/bin, I will use this a lot.
评论 #5749970 未加载
评论 #5749841 未加载
zokieralmost 12 years ago
what's the rationale behind 'gr foo -r bar' instead of simply 'gr foo bar'?
评论 #5755464 未加载
gglanzanialmost 12 years ago
Are 32bit binaries coming? For the rest, great job!
评论 #5749869 未加载
评论 #5749815 未加载