Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace many lines in a file #163

Open
gedw99 opened this issue Jan 15, 2023 · 3 comments
Open

Replace many lines in a file #163

gedw99 opened this issue Jan 15, 2023 · 3 comments

Comments

@gedw99
Copy link
Contributor

gedw99 commented Jan 15, 2023

Love this project t - thanks for doing it.

Is there a formal way of replacing many consecutive lines in a file ?

There seems to only the ability to replace single lines?

My uses case is needing to edit a file . I have a cli and gui that first copies every line of a file to the clients and holds each line in memory. Then as the user edits a line or a collection of lines and the changes are sent to be replaced in the file.

An interesting side aspect is that the user could also manually edit the file , so I also need to tail the file and send chsnged lines to the cli and gui , so the clients sone get out of sync.
I don’t think there is a formal tail command ?

@bitfield
Copy link
Owner

Hi @gedw99! No, there isn't currently a way to do a multi-line replace. The Last method works like tail, but it probably won't be helpful for you, since you need to detect any changes to the file. The script package is really aimed at the case where you filter the data once, a line at a time, rather than random-access editing of the kind you're describing.

I hope it can still be useful in some way, though.

@gedw99
Copy link
Contributor Author

gedw99 commented Mar 13, 2023

Thanks

I Kind o worked out a way by round tripping the line number throughout the event loop. So i just need to hold that line number at the Context level and so the Actor is charge of the file system knows where to inject the file change.

I also thought if using go-git but it’s YAGNI I think.

When many clients I keep all client up to data in real time with what data maps to what kind so I avoid clashes. If 2 clients are editing the same line I can do inner lines diff / replace in real time.

ohhh it’s a complicato !! Poor man’s OT / CRDT …

I worked out that the JSONLines spec is a pretty good match if working with json.

everythibg worth their salt supports it .

some of this might be refactorable into a Upstream PR, if your into the idea ..

@rmasci
Copy link

rmasci commented May 2, 2023

Actually there is a really easy way to do this currently.

... code...

pipe.File("inFile").Match("someFilter").MatchRegexp(reFinedRegex).FilterLine(modline).WriteFile("inFile")
... more code...

func modline(line string) {
    return strings.ReplaceAll(line, "before", "after")
}

This is a very simple example, the point is modline is a function and that function can be as complex as you need it to be, and whatever is returned from that function gets written to the file. The match / regexp gives me that block of multiple lines out of the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants